blob: efc0c85a97f7dee34798de796ed2a1de443e2dc3 [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file main_ni.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang's yanglint tool - noninteractive code
5 *
6 * Copyright (c) 2015-2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcied5acc52019-04-25 15:57:04 +020016
17#include <stdio.h>
18#include <stdlib.h>
19#include <getopt.h>
20#include <errno.h>
21#include <libgen.h>
22#include <sys/stat.h>
23#include <sys/times.h>
24#include <sys/types.h>
25#include <string.h>
26#include <unistd.h>
27
Radek Krejcied5acc52019-04-25 15:57:04 +020028#include "libyang.h"
29
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tools/config.h"
31
32#include "commands.h"
33
34
Radek Krejcied5acc52019-04-25 15:57:04 +020035volatile uint8_t verbose = 0;
36
37#if 0
38/* from commands.c */
39int print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
40#endif
41
42void
43help(int shortout)
44{
45 fprintf(stdout, "Usage:\n");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080046 fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | info}] <file>...\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020047 fprintf(stdout, " Validates the YANG module in <file>, and all its dependencies.\n\n");
48 fprintf(stdout, " yanglint [options] [-f { xml | json }] <schema>... <file>...\n");
49 fprintf(stdout, " Validates the YANG modeled data in <file> according to the <schema>.\n\n");
50 fprintf(stdout, " yanglint\n");
51 fprintf(stdout, " Starts interactive mode with more features.\n\n");
52
53 if (shortout) {
54 return;
55 }
56 fprintf(stdout, "Options:\n"
57 " -h, --help Show this help message and exit.\n"
58 " -v, --version Show version number and exit.\n"
59 " -V, --verbose Show verbose messages, can be used multiple times to\n"
60 " increase verbosity.\n"
61#ifndef NDEBUG
62 " -G GROUPS, --debug=GROUPS\n"
63 " Enable printing of specific debugging message group\n"
64 " (nothing will be printed unless verbosity is set to debug):\n"
65 " <group>[,<group>]* (dict, yang, yin, xpath, diff)\n\n"
66#endif
67 " -p PATH, --path=PATH Search path for schema (YANG/YIN) modules. The option can be used multiple times.\n"
68 " Current working directory and path of the module being added is used implicitly.\n\n"
69 " -D, --disable-searchdir\n"
70 " Do not implicitly search in CWD for schema modules. If specified a second time,\n"
71 " do not even search the module directory (all modules must be explicitly specified).\n\n"
72 " -s, --strict Strict data parsing (do not skip unknown data),\n"
73 " has no effect for schemas.\n\n"
74 " -m, --merge Merge input data files into a single tree and validate at once,\n"
75 " has no effect for the auto, rpc, rpcreply and notif TYPEs.\n\n"
76 " -f FORMAT, --format=FORMAT\n"
77 " Convert to FORMAT. Supported formats: \n"
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080078 " yang, yin, tree and info for schemas,\n"
Radek Krejcied5acc52019-04-25 15:57:04 +020079 " xml, json for data.\n"
80 " -a, --auto Modify the xml output by adding envelopes for autodetection.\n\n"
81 " -i, --allimplemented Make all the imported modules implemented.\n\n"
82 " -l, --list Print info about the loaded schemas in ietf-yang-library format,\n"
83 " the -f option applies here to specify data encoding.\n"
84 " (i - imported module, I - implemented module)\n\n"
85 " -o OUTFILE, --output=OUTFILE\n"
86 " Write the output to OUTFILE instead of stdout.\n\n"
87 " -F FEATURES, --features=FEATURES\n"
88 " Features to support, default all.\n"
89 " <modname>:[<feature>,]*\n\n"
90 " -d MODE, --default=MODE\n"
91 " Print data with default values, according to the MODE\n"
92 " (to print attributes, ietf-netconf-with-defaults model\n"
93 " must be loaded):\n"
94 " all - Add missing default nodes.\n"
95 " all-tagged - Add missing default nodes and mark all the default\n"
96 " nodes with the attribute.\n"
97 " trim - Remove all nodes with a default value.\n"
98 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"
99 " -t TYPE, --type=TYPE\n"
100 " Specify data tree type in the input data file:\n"
101 " auto - Resolve data type (one of the following) automatically\n"
102 " (as pyang does) - applicable only on XML input data.\n"
103 " data - Complete datastore with status data (default type).\n"
104 " config - Configuration datastore (without status data).\n"
105 " get - Result of the NETCONF <get> operation.\n"
106 " getconfig - Result of the NETCONF <get-config> operation.\n"
107 " edit - Content of the NETCONF <edit-config> operation.\n"
108 " rpc - Content of the NETCONF <rpc> message, defined as YANG's rpc input statement.\n"
109 " rpcreply - Reply to the RPC. The input data <file>s are expected in pairs - each RPC reply\n"
110 " input data <file> must be followed by the origin RPC input data <file> for the reply.\n"
111 " The same rule of pairing applies also in case of 'auto' TYPE and input data file\n"
112 " containing RPC reply.\n"
113 " notif - Notification instance (content of the <notification> element without <eventTime>.\n\n"
114 " -O FILE, --operational=FILE\n"
115 " - Optional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the FILE contains running\n"
116 " configuration datastore and state data (operational datastore) referenced from\n"
117 " the RPC/Notification. The same data apply to all input data <file>s. Note that\n"
118 " the file is validated as 'data' TYPE. Special value '!' can be used as FILE argument\n"
119 " to ignore the external references.\n\n"
120 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
121 "Tree output specific options:\n"
122 " --tree-help - Print help on tree symbols and exit.\n"
123 " --tree-print-groupings\n"
124 " Print top-level groupings in a separate section.\n"
125 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
126 " --tree-no-leafref-target\n"
127 " Do not print target nodes of leafrefs.\n"
128 " --tree-path=SCHEMA_PATH\n"
129 " Print only the specified subtree.\n"
130 " --tree-line-length=LINE_LENGTH\n"
131 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800132 " can often appear).\n\n"
133 "Info output specific options:\n"
134 " -P INFOPATH, --info-path=INFOPATH\n"
135 " - Schema path with full module names used as node's prefixes, the path identify the root\n"
136 " node of the subtree to print information about.\n"
137 " --single-node - Print information about a single node instead of a subtree."
Radek Krejcied5acc52019-04-25 15:57:04 +0200138 "\n");
139}
140
141void
142tree_help(void)
143{
144 fprintf(stdout, "Each node is printed as:\n\n");
145 fprintf(stdout, "<status> <flags> <name> <opts> <type> <if-features>\n\n"
146 " <status> is one of:\n"
147 " + for current\n"
148 " x for deprecated\n"
149 " o for obsolete\n\n"
150 " <flags> is one of:\n"
151 " rw for configuration data\n"
152 " ro for status data\n"
153 " -x for RPCs\n"
154 " -n for Notification\n\n"
155 " <name> is the name of the node\n"
156 " (<name>) means that the node is a choice node\n"
157 " :(<name>) means that the node is a case node\n\n"
158 " if the node is augmented into the tree from another module,\n"
159 " it is printed with the module name as <module-name>:<name>.\n\n"
160 " <opts> is one of:\n"
161 " ? for an optional leaf or choice\n"
162 " ! for a presence container\n"
163 " * for a leaf-list or list\n"
164 " [<keys>] for a list's keys\n\n"
165 " <type> is the name of the type for leafs and leaf-lists\n"
166 " If there is a default value defined, it is printed within\n"
167 " angle brackets <default-value>.\n"
168 " If the type is a leafref, the type is printed as -> TARGET`\n\n"
169 " <if-features> is the list of features this node depends on,\n"
170 " printed within curly brackets and a question mark {...}?\n\n");
171}
172
173void
174version(void)
175{
176 fprintf(stdout, "yanglint %s\n", PROJECT_VERSION);
177}
178
179void
180libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
181{
182 char *levstr;
183
184 if (level <= verbose) {
185 switch(level) {
186 case LY_LLERR:
187 levstr = "err :";
188 break;
189 case LY_LLWRN:
190 levstr = "warn:";
191 break;
192 case LY_LLVRB:
193 levstr = "verb:";
194 break;
195 default:
196 levstr = "dbg :";
197 break;
198 }
199 if (path) {
200 fprintf(stderr, "%s %s (%s)\n", levstr, msg, path);
201 } else {
202 fprintf(stderr, "%s %s\n", levstr, msg);
203 }
204 }
205}
206
207/*
208 * return:
209 * 0 - error
210 * 1 - schema format
211 * 2 - data format
212 */
213static int
Radek Krejcie7b95092019-05-15 11:03:07 +0200214get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
Radek Krejcied5acc52019-04-25 15:57:04 +0200215{
216 char *ptr;
217 LYS_INFORMAT informat_s;
Radek Krejcied5acc52019-04-25 15:57:04 +0200218 LYD_FORMAT informat_d;
Radek Krejcie7b95092019-05-15 11:03:07 +0200219
Radek Krejcied5acc52019-04-25 15:57:04 +0200220 /* get the file format */
221 if ((ptr = strrchr(filename, '.')) != NULL) {
222 ++ptr;
223 if (!strcmp(ptr, "yang")) {
224 informat_s = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200225 informat_d = 0;
226 } else if (!strcmp(ptr, "yin")) {
227 informat_s = LYS_IN_YIN;
228 informat_d = 0;
229 } else if (!strcmp(ptr, "xml")) {
230 informat_s = 0;
231 informat_d = LYD_XML;
232 } else if (!strcmp(ptr, "json")) {
233 informat_s = 0;
234 informat_d = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200235 } else {
236 fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr);
237 return 0;
238 }
239 } else {
240 fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
241 return 0;
242 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200243
Radek Krejcied5acc52019-04-25 15:57:04 +0200244 if (data) {
245 (*data) = informat_d;
246 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200247
Radek Krejcied5acc52019-04-25 15:57:04 +0200248 if (schema) {
249 (*schema) = informat_s;
250 }
251
252 if (informat_s) {
253 return 1;
254 } else {
255 return 2;
256 }
257}
258
259int
260main_ni(int argc, char* argv[])
261{
262 int ret = EXIT_FAILURE;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800263 int opt, opt_index = 0, i, featsize = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200264 struct option options[] = {
265#if 0
266 {"auto", no_argument, NULL, 'a'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200267#endif
Radek Krejciff61c882020-09-03 13:04:18 +0200268 {"default", required_argument, NULL, 'd'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200269 {"format", required_argument, NULL, 'f'},
270 {"features", required_argument, NULL, 'F'},
271#if 0
272 {"tree-print-groupings", no_argument, NULL, 'g'},
273 {"tree-print-uses", no_argument, NULL, 'u'},
274 {"tree-no-leafref-target", no_argument, NULL, 'n'},
275 {"tree-path", required_argument, NULL, 'P'},
276 {"tree-line-length", required_argument, NULL, 'L'},
277#endif
278 {"help", no_argument, NULL, 'h'},
279#if 0
280 {"tree-help", no_argument, NULL, 'H'},
281#endif
282 {"allimplemented", no_argument, NULL, 'i'},
283 {"disable-cwd-search", no_argument, NULL, 'D'},
284 {"list", no_argument, NULL, 'l'},
285#if 0
286 {"merge", no_argument, NULL, 'm'},
287#endif
288 {"output", required_argument, NULL, 'o'},
289 {"path", required_argument, NULL, 'p'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800290 {"info-path", required_argument, NULL, 'P'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200291#if 0
292 {"running", required_argument, NULL, 'r'},
293 {"operational", required_argument, NULL, 'O'},
Radek Krejcie7b95092019-05-15 11:03:07 +0200294#endif
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800295 {"single-node", no_argument, NULL, 'q'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200296 {"strict", no_argument, NULL, 's'},
297 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200298 {"version", no_argument, NULL, 'v'},
299 {"verbose", no_argument, NULL, 'V'},
300#ifndef NDEBUG
301 {"debug", required_argument, NULL, 'G'},
302#endif
303 {NULL, required_argument, NULL, 'y'},
304 {NULL, 0, NULL, 0}
305 };
Radek Krejci241f6b52020-05-21 18:13:49 +0200306 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200307 struct ly_ctx *ctx = NULL;
308 const struct lys_module *mod;
309 LYS_OUTFORMAT outformat_s = 0;
310 LYS_INFORMAT informat_s;
Radek Krejcie7b95092019-05-15 11:03:07 +0200311 LYD_FORMAT informat_d, outformat_d = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200312#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 LYD_FORMAT ylformat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200314#endif
315 struct ly_set *searchpaths = NULL;
316 const char *outtarget_s = NULL;
317 char **feat = NULL, *ptr, *featlist, *dir;
318 struct stat st;
319 uint32_t u;
320 int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 int autodetection = 0, options_parser = 0, merge = 0;
322 const char *oper_file = NULL;
Radek Krejciff61c882020-09-03 13:04:18 +0200323 int options_dflt = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200324#if 0
Radek Krejciff61c882020-09-03 13:04:18 +0200325 ly_bool envelope = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200326 const char *envelope_s = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200327 char *ylpath = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 struct lyxml_elem *iter, *elem;
329 struct *subroot, *next, *node;
330#endif
Michal Vaskof03ed032020-03-04 13:31:44 +0100331 struct lyd_node *tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200332 struct dataitem {
333 const char *filename;
Radek Krejcied5acc52019-04-25 15:57:04 +0200334 struct lyd_node *tree;
335 struct dataitem *next;
336 LYD_FORMAT format;
Michal Vaskoa3881362020-01-21 15:57:35 +0100337 int flags;
Radek Krejcied5acc52019-04-25 15:57:04 +0200338 } *data = NULL, *data_item, *data_prev = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200339 struct ly_set *mods = NULL;
340 void *p;
Radek Krejcied5acc52019-04-25 15:57:04 +0200341
342 opterr = 0;
343#ifndef NDEBUG
Radek Krejci733811f2020-09-04 10:15:10 +0200344 while ((opt = getopt_long(argc, argv, "acd:f:F:gunP:L:hHiDlmo:p:O:st:vVG:y:", options, &opt_index)) != -1)
Radek Krejcied5acc52019-04-25 15:57:04 +0200345#else
Radek Krejci733811f2020-09-04 10:15:10 +0200346 while ((opt = getopt_long(argc, argv, "acd:f:F:gunP:L:hHiDlmo:p:O:st:vVy:", options, &opt_index)) != -1)
Radek Krejcied5acc52019-04-25 15:57:04 +0200347#endif
348 {
349 switch (opt) {
350#if 0
Radek Krejciff61c882020-09-03 13:04:18 +0200351 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200352 case 'a':
353 envelope = 1;
354 break;
Radek Krejciff61c882020-09-03 13:04:18 +0200355#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200356 case 'd':
357 if (!strcmp(optarg, "all")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200358 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200359 } else if (!strcmp(optarg, "all-tagged")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200360 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200361 } else if (!strcmp(optarg, "trim")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200362 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
Radek Krejcied5acc52019-04-25 15:57:04 +0200363 } else if (!strcmp(optarg, "implicit-tagged")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200364 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200365 } else {
366 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
367 help(1);
368 goto cleanup;
369 }
370 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200371 case 'f':
372 if (!strcasecmp(optarg, "yang")) {
373 outformat_s = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200374 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200375#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200376 } else if (!strcasecmp(optarg, "tree")) {
377 outformat_s = LYS_OUT_TREE;
378 outformat_d = 0;
379 } else if (!strcasecmp(optarg, "tree-rfc")) {
380 outformat_s = LYS_OUT_TREE;
381 outoptions_s |= LYS_OUTOPT_TREE_RFC;
382 outformat_d = 0;
FredGand944bdc2019-11-05 21:57:07 +0800383#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200384 } else if (!strcasecmp(optarg, "yin")) {
385 outformat_s = LYS_OUT_YIN;
386 outformat_d = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800387 } else if (!strcasecmp(optarg, "info")) {
388 outformat_s = LYS_OUT_YANG_COMPILED;
Radek Krejcied5acc52019-04-25 15:57:04 +0200389 outformat_d = 0;
390 } else if (!strcasecmp(optarg, "xml")) {
391 outformat_s = 0;
392 outformat_d = LYD_XML;
393 } else if (!strcasecmp(optarg, "json")) {
394 outformat_s = 0;
395 outformat_d = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200396 } else {
397 fprintf(stderr, "yanglint error: unknown output format %s\n", optarg);
398 help(1);
399 goto cleanup;
400 }
401 break;
402 case 'F':
403 featsize++;
404 if (!feat) {
405 p = malloc(sizeof *feat);
406 } else {
407 p = realloc(feat, featsize * sizeof *feat);
408 }
409 if (!p) {
410 fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
411 goto cleanup;
412 }
413 feat = p;
414 feat[featsize - 1] = strdup(optarg);
415 ptr = strchr(feat[featsize - 1], ':');
416 if (!ptr) {
417 fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg);
418 goto cleanup;
419 }
420 *ptr = '\0';
421
422 break;
423#if 0
424 case 'g':
425 outoptions_s |= LYS_OUTOPT_TREE_GROUPING;
426 break;
427 case 'u':
428 outoptions_s |= LYS_OUTOPT_TREE_USES;
429 break;
430 case 'n':
431 outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF;
432 break;
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800433#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200434 case 'P':
435 outtarget_s = optarg;
436 break;
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800437#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200438 case 'L':
439 outline_length_s = atoi(optarg);
440 break;
441#endif
442 case 'h':
443 help(0);
444 ret = EXIT_SUCCESS;
445 goto cleanup;
446#if 0
447 case 'H':
448 tree_help();
449 ret = EXIT_SUCCESS;
450 goto cleanup;
451#endif
452 case 'i':
453 options_ctx |= LY_CTX_ALLIMPLEMENTED;
454 break;
455 case 'D':
456 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
457 fprintf(stderr, "yanglint error: -D specified too many times.\n");
458 goto cleanup;
459 } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
460 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
461 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
462 } else {
463 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
464 }
465 break;
466 case 'l':
467 list = 1;
468 break;
469#if 0
470 case 'm':
471 merge = 1;
472 break;
473#endif
474 case 'o':
Radek Krejcia5bba312020-01-09 15:41:20 +0100475 if (out) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200476 if (ly_out_filepath(out, optarg) != NULL) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100477 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
478 goto cleanup;
479 }
480 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200481 if (ly_out_new_filepath(optarg, &out)) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100482 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
483 goto cleanup;
484 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200485 }
486 break;
487 case 'p':
488 if (stat(optarg, &st) == -1) {
489 fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
490 goto cleanup;
491 }
492 if (!S_ISDIR(st.st_mode)) {
493 fprintf(stderr, "yanglint error: Provided search path is not a directory.\n");
494 goto cleanup;
495 }
496 if (!searchpaths) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200497 if (ly_set_new(&searchpaths)) {
498 fprintf(stderr, "yanglint error: Preparing storage for searchpaths failed.\n");
499 goto cleanup;
500 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200501 }
Radek Krejciba03a5a2020-08-27 14:40:41 +0200502 if (ly_set_add(searchpaths, optarg, 0, NULL)) {
503 fprintf(stderr, "yanglint error: Storing searchpath failed.\n");
504 goto cleanup;
505 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200506 break;
507#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200508 case 'O':
509 if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) {
510 fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n");
511 goto cleanup;
512 }
513 if (optarg[0] == '!') {
514 /* ignore extenral dependencies to the operational datastore */
515 options_parser |= LYD_OPT_NOEXTDEPS;
516 } else {
517 /* external file with the operational datastore */
518 oper_file = optarg;
519 }
520 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200521#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200522 case 's':
Radek Krejci7931b192020-06-25 17:05:03 +0200523 options_parser |= LYD_PARSE_STRICT;
Radek Krejcied5acc52019-04-25 15:57:04 +0200524 break;
525 case 't':
526 if (!strcmp(optarg, "auto")) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200527 autodetection = 1;
Michal Vaskob36053d2020-03-26 15:49:30 +0100528 /*} else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100529 options_parser |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200530 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100531 options_parser |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200532 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100533 options_parser |= LYD_OPT_GETCONFIG;
Michal Vaskob36053d2020-03-26 15:49:30 +0100534 } else if (!strcmp(optarg, "edit")) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100535 options_parser |= LYD_OPT_EDIT;*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200536 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100537 /* no options */
Radek Krejcied5acc52019-04-25 15:57:04 +0200538 } else {
539 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
540 help(1);
541 goto cleanup;
542 }
543 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200544 case 'v':
545 version();
546 ret = EXIT_SUCCESS;
547 goto cleanup;
548 case 'V':
549 verbose++;
550 break;
551#ifndef NDEBUG
552 case 'G':
553 u = 0;
554 ptr = optarg;
555 while (ptr[0]) {
556 if (!strncmp(ptr, "dict", 4)) {
557 u |= LY_LDGDICT;
558 ptr += 4;
559 } else if (!strncmp(ptr, "yang", 4)) {
560 u |= LY_LDGYANG;
561 ptr += 4;
562 } else if (!strncmp(ptr, "yin", 3)) {
563 u |= LY_LDGYIN;
564 ptr += 3;
565 } else if (!strncmp(ptr, "xpath", 5)) {
566 u |= LY_LDGXPATH;
567 ptr += 5;
568 } else if (!strncmp(ptr, "diff", 4)) {
569 u |= LY_LDGDIFF;
570 ptr += 4;
571 }
572
573 if (ptr[0]) {
574 if (ptr[0] != ',') {
575 fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg);
576 goto cleanup;
577 }
578 ++ptr;
579 }
580 }
581 ly_verb_dbg(u);
582 break;
583#endif
584#if 0
585 case 'y':
586 ptr = strrchr(optarg, '.');
587 if (ptr) {
588 ptr++;
589 if (!strcmp(ptr, "xml")) {
590 ylformat = LYD_XML;
591 } else if (!strcmp(ptr, "json")) {
592 ylformat = LYD_JSON;
593 } else {
594 fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr);
595 goto cleanup;
596 }
597 } else {
598 fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n");
599 goto cleanup;
600 }
601 ylpath = optarg;
602 break;
603#endif
604 default:
605 help(1);
606 if (optopt) {
607 fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt);
608 } else {
609 fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]);
610 }
611 goto cleanup;
612 }
613 }
614
615 /* check options compatibility */
616 if (!list && optind >= argc) {
617 help(1);
618 fprintf(stderr, "yanglint error: missing <file> to process\n");
619 goto cleanup;
620 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200621 if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) {
622 /* we have multiple schemas to be printed as YIN or YANG */
623 fprintf(stderr, "yanglint error: too many schemas to convert and store.\n");
624 goto cleanup;
625 }
626 if (outoptions_s || outtarget_s || outline_length_s) {
627#if 0
628 if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) {
629 /* we have --tree-print-grouping with other output format than tree */
630 fprintf(stderr,
631 "yanglint warning: --tree options take effect only in case of the tree output format.\n");
632 }
633 }
634 if (merge) {
635 if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
636 fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n");
637 merge = 0;
638 } else {
639 /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged
640 * and the result will be validated */
641 options_parser |= LYD_OPT_TRUSTED;
642 }
643#endif
644 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200645 if (!outformat_d && options_dflt) {
646 /* we have options for printing default nodes, but data output not specified */
647 fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
648 }
Radek Krejciff61c882020-09-03 13:04:18 +0200649#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200650 if (outformat_s && (options_parser || autodetection)) {
651 /* we have options for printing data tree, but output is schema */
652 fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
653 }
654 if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
655 fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n");
656 /* ignore operational datastore file */
657 oper_file = NULL;
658 }
659 if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
660 /* add option to ignore ietf-yang-library data for implicit data type */
661 options_parser |= LYD_OPT_DATA_NO_YANGLIB;
662 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100663#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200664
665 /* set callback for printing libyang messages */
666 ly_set_log_clb(libyang_verbclb, 1);
667#if 0
668 /* create libyang context */
669 if (ylpath) {
670 ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx);
671 } else {
672#else
673 {
674#endif
675 ly_ctx_new(NULL, options_ctx, &ctx);
676 }
677 if (!ctx) {
678 goto cleanup;
679 }
680
681 /* set searchpaths */
682 if (searchpaths) {
683 for (u = 0; u < searchpaths->count; u++) {
684 ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]);
685 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200686 }
687
688 /* derefered setting of verbosity in libyang after context initiation */
689 ly_verb(verbose);
690
Radek Krejciba03a5a2020-08-27 14:40:41 +0200691 if (ly_set_new(&mods)) {
692 fprintf(stderr, "yanglint error: Preparing storage for the parsed modules failed.\n");
693 goto cleanup;
694 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200695
696
697 /* divide input files */
698 for (i = 0; i < argc - optind; i++) {
699 /* get the file format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200700 if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200701 goto cleanup;
702 }
703
704 if (informat_s) {
705 /* load/validate schema */
Radek Krejci83126492020-08-15 15:40:04 +0200706 int unset_path = 1;
707
Radek Krejcied5acc52019-04-25 15:57:04 +0200708 if (verbose >= 2) {
709 fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]);
710 }
Radek Krejci83126492020-08-15 15:40:04 +0200711
712 /* add temporarily also the path of the module itself */
Radek Krejcied5acc52019-04-25 15:57:04 +0200713 dir = strdup(argv[optind + i]);
Radek Krejci83126492020-08-15 15:40:04 +0200714 if (ly_ctx_set_searchdir(ctx, ptr = dirname(dir)) == LY_EEXIST) {
715 unset_path = 0;
716 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200717 lys_parse_path(ctx, argv[optind + i], informat_s, &mod);
Radek Krejcie58f97f2020-08-18 11:45:08 +0200718 ly_ctx_unset_searchdir_last(ctx, unset_path);
Radek Krejcied5acc52019-04-25 15:57:04 +0200719 free(dir);
720 if (!mod) {
721 goto cleanup;
722 }
Radek Krejciba03a5a2020-08-27 14:40:41 +0200723 if (ly_set_add(mods, (void *)mod, 0, NULL)) {
724 fprintf(stderr, "yanglint error: Storing parsed module for further processing failed.\n");
725 goto cleanup;
726 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200727 } else {
728 if (autodetection && informat_d != LYD_XML) {
729 /* data file content autodetection is possible only for XML input */
730 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
731 goto cleanup;
732 }
733
734 /* remember data filename and its format */
735 if (!data) {
736 data = data_item = malloc(sizeof *data);
737 } else {
738 for (data_item = data; data_item->next; data_item = data_item->next);
739 data_item->next = malloc(sizeof *data_item);
740 data_item = data_item->next;
741 }
742 data_item->filename = argv[optind + i];
743 data_item->format = informat_d;
Michal Vaskoa3881362020-01-21 15:57:35 +0100744 data_item->flags = options_parser;
Radek Krejcied5acc52019-04-25 15:57:04 +0200745 data_item->tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200746 data_item->next = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200747 }
748 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200749 if (outformat_d && !data && !list) {
750 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
751 goto cleanup;
752 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200753
754 /* enable specified features, if not specified, all the module's features are enabled */
755 u = 4; /* skip internal libyang modules */
756 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci3d46f612020-08-13 12:01:53 +0200757 if (!mod->implemented) {
758 continue;
759 }
760
Radek Krejcied5acc52019-04-25 15:57:04 +0200761 for (i = 0; i < featsize; i++) {
762 if (!strcmp(feat[i], mod->name)) {
763 /* parse features spec */
764 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
765 ptr = NULL;
766 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
767 if (verbose >= 2) {
768 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
769 }
770 if (lys_feature_enable(mod, ptr)) {
771 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
772 }
773 }
774 free(featlist);
775 break;
776 }
777 }
778 if (i == featsize) {
779 if (verbose >= 2) {
780 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
781 }
782 lys_feature_enable(mod, "*");
783 }
784 }
Radek Krejci460937d2020-09-04 10:15:36 +0200785 if (!out && (outformat_s || data)) {
786 ly_out_new_file(stdout, &out);
787 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 /* convert (print) to FORMAT */
789 if (outformat_s) {
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800790 if (outtarget_s) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100791 const struct lysc_node *node = lys_find_node(ctx, NULL, outtarget_s);
792 if (node) {
793 lys_print_node(out, node, outformat_s, outline_length_s, outoptions_s);
794 } else {
795 fprintf(stderr, "yanglint error: The requested schema node \"%s\" does not exists.\n", outtarget_s);
796 }
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800797 } else {
798 for (u = 0; u < mods->count; u++) {
799 if (u) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200800 ly_print(out, "\n");
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800801 }
Michal Vasko7c8439f2020-08-05 13:25:19 +0200802 lys_print_module(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
Radek Krejcied5acc52019-04-25 15:57:04 +0200803 }
804 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200805 } else if (data) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200806
807 /* prepare operational datastore when specified for RPC/Notification */
808 if (oper_file) {
Radek Krejci7931b192020-06-25 17:05:03 +0200809 struct ly_in *in;
810 tree = NULL;
811
812 if (ly_in_new_filepath(oper_file, 0, &in)) {
813 fprintf(stderr, "yanglint error: Unable to open an operational data file \"%s\".\n", oper_file);
Radek Krejcied5acc52019-04-25 15:57:04 +0200814 goto cleanup;
815 }
Radek Krejci7931b192020-06-25 17:05:03 +0200816 if (lyd_parse_data(ctx, in, 0, LYD_PARSE_ONLY, 0, &tree) || !tree) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200817 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
Radek Krejci7931b192020-06-25 17:05:03 +0200818 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200819 goto cleanup;
820 }
Radek Krejci7931b192020-06-25 17:05:03 +0200821 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200822 }
823
824 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
825 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
Radek Krejcie7b95092019-05-15 11:03:07 +0200826#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200827 if (autodetection) {
828 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
829 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
830 /* automatically detect data type from the data top level */
831 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
832 if (!data_item->xml) {
833 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
834 goto cleanup;
835 }
836
837 /* NOTE: namespace is ignored to simplify usage of this feature */
838 if (!strcmp(data_item->xml->name, "data")) {
839 if (verbose >= 2) {
840 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
841 }
842 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
843 data_item->type = LYD_OPT_DATA;
844 } else if (!strcmp(data_item->xml->name, "config")) {
845 if (verbose >= 2) {
846 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
847 }
848 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
849 data_item->type = LYD_OPT_CONFIG;
850 } else if (!strcmp(data_item->xml->name, "get-reply")) {
851 if (verbose >= 2) {
852 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
853 }
854 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
855 data_item->type = LYD_OPT_GET;
856 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
857 if (verbose >= 2) {
858 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
859 }
860 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
861 data_item->type = LYD_OPT_GETCONFIG;
862 } else if (!strcmp(data_item->xml->name, "edit-config")) {
863 if (verbose >= 2) {
864 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
865 }
866 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
867 data_item->type = LYD_OPT_EDIT;
868 } else if (!strcmp(data_item->xml->name, "rpc")) {
869 if (verbose >= 2) {
870 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
871 }
872 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
873 data_item->type = LYD_OPT_RPC;
874 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
875 if (verbose >= 2) {
876 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
877 }
878
879 data_item->type = LYD_OPT_RPCREPLY;
880 if (!data_item->next || (data_prev && !data_prev->tree)) {
881 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
882 goto cleanup;
883 }
884
885 continue;
886 } else if (!strcmp(data_item->xml->name, "notification")) {
887 if (verbose >= 2) {
888 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
889 }
890 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
891 data_item->type = LYD_OPT_NOTIF;
892
893 /* ignore eventTime element if present */
894 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
895 lyxml_free(ctx, data_item->xml->child);
896 }
897 } else {
898 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
899 data_item->xml->name);
900 goto cleanup;
901 }
902
903 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
904 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
905parse_reply:
906 /* check result of the RPC parsing, we are going to do another parsing in this step */
907 if (ly_errno) {
908 goto cleanup;
909 }
910
911 /* check that we really have RPC for the reply */
912 if (data_item->type != LYD_OPT_RPC) {
913 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
914 goto cleanup;
915 }
916
917 if (data_prev->format == LYD_XML) {
918 /* ignore <ok> and <rpc-error> elements if present */
919 u = 0;
920 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
921 if (!strcmp(data_prev->xml->child->name, "ok")) {
922 if (u) {
923 /* rpc-error or ok already present */
924 u = 0x8; /* error flag */
925 } else {
926 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
927 }
928 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
929 if (u && (u & 0x1)) {
930 /* ok already present, rpc-error can be present multiple times */
931 u = 0x8; /* error flag */
932 } else {
933 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
934 }
935 }
936
937 if (u == 0x8) {
938 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
939 goto cleanup;
940 } else if (u & 0x4) {
941 lyxml_free(ctx, data_prev->xml->child);
942 u &= ~0x4; /* unset lyxml_free() flag */
943 }
944 }
945
946 /* finally, parse RPC reply from the previous step */
947 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
948 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
949 } else { /* LYD_JSON */
950 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
951 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
952 }
953 }
954 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
955 if (data_prev && !data_prev->tree) {
956 /* now we should have RPC for the preceding RPC reply */
957 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
958 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
959 data_item->type = LYD_OPT_RPC;
960 goto parse_reply;
961 } else {
962 /* now we have RPC reply which will be parsed in next step together with its RPC */
963 if (!data_item->next) {
964 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
965 goto cleanup;
966 }
967 if (data_item->format == LYD_XML) {
968 /* create rpc-reply container to unify handling with autodetection */
969 data_item->xml = calloc(1, sizeof *data_item->xml);
970 if (!data_item->xml) {
971 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
972 goto cleanup;
973 }
974 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
975 data_item->xml->prev = data_item->xml;
976 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
977 if (data_item->xml->child) {
978 data_item->xml->child->parent = data_item->xml;
979 }
980 }
981 continue;
982 }
983 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200984#else
985 {
986#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200987 /* TODO optimize use of ly_in in the loop */
988 struct ly_in *in;
989 if (ly_in_new_filepath(data_item->filename, 0, &in)) {
990 fprintf(stderr, "yanglint error: input data file \"%s\".\n", data_item->filename);
991 goto cleanup;
992 }
Radek Krejci5536d282020-08-04 23:27:44 +0200993 if (lyd_parse_data(ctx, in, 0, options_parser, LYD_VALIDATE_PRESENT, &data_item->tree)) {
Radek Krejci7931b192020-06-25 17:05:03 +0200994 fprintf(stderr, "yanglint error: Failed to parse input data file \"%s\".\n", data_item->filename);
995 ly_in_free(in, 0);
996 goto cleanup;
997 }
998 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200999 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001000#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001001 if (merge && data != data_item) {
1002 if (!data->tree) {
1003 data->tree = data_item->tree;
1004 } else if (data_item->tree) {
1005 /* merge results */
1006 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
1007 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
1008 goto cleanup;
1009 }
1010 }
1011 data_item->tree = NULL;
1012 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001013#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001014 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001015#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001016 if (merge) {
1017 /* validate the merged data tree, do not trust the input, invalidate all the data first */
1018 LY_TREE_FOR(data->tree, subroot) {
1019 LY_TREE_DFS_BEGIN(subroot, next, node) {
1020 node->validity = LYD_VAL_OK;
1021 switch (node->schema->nodetype) {
1022 case LYS_LEAFLIST:
1023 case LYS_LEAF:
1024 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1025 node->validity |= LYD_VAL_LEAFREF;
1026 }
1027 break;
1028 case LYS_LIST:
1029 node->validity |= LYD_VAL_UNIQUE;
1030 /* falls through */
1031 case LYS_CONTAINER:
1032 case LYS_NOTIF:
1033 case LYS_RPC:
1034 case LYS_ACTION:
1035 node->validity |= LYD_VAL_MAND;
1036 break;
1037 default:
1038 break;
1039 }
1040 LY_TREE_DFS_END(subroot, next, node)
1041 }
1042 }
1043 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1044 goto cleanup;
1045 }
1046 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001047#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001048 /* print only if data output format specified */
1049 if (outformat_d) {
1050 for (data_item = data; data_item; data_item = data_item->next) {
1051 if (!merge && verbose >= 2) {
1052 fprintf(stdout, "File %s:\n", data_item->filename);
1053 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001054#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001055 if (outformat_d == LYD_XML && envelope) {
1056 switch (data_item->type) {
1057 case LYD_OPT_DATA:
1058 envelope_s = "data";
1059 break;
1060 case LYD_OPT_CONFIG:
1061 envelope_s = "config";
1062 break;
1063 case LYD_OPT_GET:
1064 envelope_s = "get-reply";
1065 break;
1066 case LYD_OPT_GETCONFIG:
1067 envelope_s = "get-config-reply";
1068 break;
1069 case LYD_OPT_EDIT:
1070 envelope_s = "edit-config";
1071 break;
1072 case LYD_OPT_RPC:
1073 envelope_s = "rpc";
1074 break;
1075 case LYD_OPT_RPCREPLY:
1076 envelope_s = "rpc-reply";
1077 break;
1078 case LYD_OPT_NOTIF:
1079 envelope_s = "notification";
1080 break;
1081 }
1082 fprintf(out, "<%s>\n", envelope_s);
1083 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1084 /* action */
1085 fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
1086 }
1087 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001088#endif
Radek Krejciff61c882020-09-03 13:04:18 +02001089 lyd_print_all(out, data_item->tree, outformat_d, options_dflt);
Radek Krejcie7b95092019-05-15 11:03:07 +02001090#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001091 if (envelope_s) {
1092 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1093 fprintf(out, "</action>\n");
1094 }
1095 fprintf(out, "</%s>\n", envelope_s);
1096 }
1097 if (merge) {
1098 /* stop after first item */
1099 break;
1100 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001101#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001102 }
1103 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001104 }
1105#if 0
1106 if (list) {
1107 print_list(out, ctx, outformat_d);
1108 }
1109#endif
1110
1111 ret = EXIT_SUCCESS;
1112
1113cleanup:
Radek Krejcied5acc52019-04-25 15:57:04 +02001114 ly_set_free(mods, NULL);
1115 ly_set_free(searchpaths, NULL);
1116 for (i = 0; i < featsize; i++) {
1117 free(feat[i]);
1118 }
1119 free(feat);
Radek Krejcied5acc52019-04-25 15:57:04 +02001120 for (; data; data = data_item) {
1121 data_item = data->next;
Radek Krejcie7b95092019-05-15 11:03:07 +02001122 lyd_free_all(data->tree);
Radek Krejcied5acc52019-04-25 15:57:04 +02001123 free(data);
1124 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001125 ly_ctx_destroy(ctx, NULL);
1126
Radek Krejci241f6b52020-05-21 18:13:49 +02001127 ly_out_free(out, NULL, 1);
Radek Krejcied5acc52019-04-25 15:57:04 +02001128 return ret;
1129}