blob: dda395d0715d7953ce8b262ad9631db926bc840e [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
Radek Krejci47fab892020-11-05 17:02:41 +010017#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <getopt.h>
21#include <errno.h>
22#include <libgen.h>
23#include <sys/stat.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020024#include <string.h>
Radek Krejci47fab892020-11-05 17:02:41 +010025#include <strings.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020026
Radek Krejcied5acc52019-04-25 15:57:04 +020027#include "libyang.h"
28
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "tools/config.h"
30
31#include "commands.h"
32
33
Radek Krejcied5acc52019-04-25 15:57:04 +020034volatile uint8_t verbose = 0;
35
36#if 0
37/* from commands.c */
38int print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat);
39#endif
40
41void
42help(int shortout)
43{
44 fprintf(stdout, "Usage:\n");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080045 fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | info}] <file>...\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020046 fprintf(stdout, " Validates the YANG module in <file>, and all its dependencies.\n\n");
47 fprintf(stdout, " yanglint [options] [-f { xml | json }] <schema>... <file>...\n");
48 fprintf(stdout, " Validates the YANG modeled data in <file> according to the <schema>.\n\n");
49 fprintf(stdout, " yanglint\n");
50 fprintf(stdout, " Starts interactive mode with more features.\n\n");
51
52 if (shortout) {
53 return;
54 }
55 fprintf(stdout, "Options:\n"
56 " -h, --help Show this help message and exit.\n"
57 " -v, --version Show version number and exit.\n"
58 " -V, --verbose Show verbose messages, can be used multiple times to\n"
59 " increase verbosity.\n"
60#ifndef NDEBUG
61 " -G GROUPS, --debug=GROUPS\n"
62 " Enable printing of specific debugging message group\n"
63 " (nothing will be printed unless verbosity is set to debug):\n"
64 " <group>[,<group>]* (dict, yang, yin, xpath, diff)\n\n"
65#endif
66 " -p PATH, --path=PATH Search path for schema (YANG/YIN) modules. The option can be used multiple times.\n"
67 " Current working directory and path of the module being added is used implicitly.\n\n"
68 " -D, --disable-searchdir\n"
69 " Do not implicitly search in CWD for schema modules. If specified a second time,\n"
70 " do not even search the module directory (all modules must be explicitly specified).\n\n"
71 " -s, --strict Strict data parsing (do not skip unknown data),\n"
72 " has no effect for schemas.\n\n"
73 " -m, --merge Merge input data files into a single tree and validate at once,\n"
74 " has no effect for the auto, rpc, rpcreply and notif TYPEs.\n\n"
75 " -f FORMAT, --format=FORMAT\n"
76 " Convert to FORMAT. Supported formats: \n"
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080077 " yang, yin, tree and info for schemas,\n"
Radek Krejcied5acc52019-04-25 15:57:04 +020078 " xml, json for data.\n"
79 " -a, --auto Modify the xml output by adding envelopes for autodetection.\n\n"
80 " -i, --allimplemented Make all the imported modules implemented.\n\n"
81 " -l, --list Print info about the loaded schemas in ietf-yang-library format,\n"
82 " the -f option applies here to specify data encoding.\n"
83 " (i - imported module, I - implemented module)\n\n"
84 " -o OUTFILE, --output=OUTFILE\n"
85 " Write the output to OUTFILE instead of stdout.\n\n"
86 " -F FEATURES, --features=FEATURES\n"
87 " Features to support, default all.\n"
88 " <modname>:[<feature>,]*\n\n"
89 " -d MODE, --default=MODE\n"
90 " Print data with default values, according to the MODE\n"
91 " (to print attributes, ietf-netconf-with-defaults model\n"
92 " must be loaded):\n"
93 " all - Add missing default nodes.\n"
94 " all-tagged - Add missing default nodes and mark all the default\n"
95 " nodes with the attribute.\n"
96 " trim - Remove all nodes with a default value.\n"
97 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"
98 " -t TYPE, --type=TYPE\n"
99 " Specify data tree type in the input data file:\n"
100 " auto - Resolve data type (one of the following) automatically\n"
101 " (as pyang does) - applicable only on XML input data.\n"
102 " data - Complete datastore with status data (default type).\n"
103 " config - Configuration datastore (without status data).\n"
104 " get - Result of the NETCONF <get> operation.\n"
105 " getconfig - Result of the NETCONF <get-config> operation.\n"
106 " edit - Content of the NETCONF <edit-config> operation.\n"
107 " rpc - Content of the NETCONF <rpc> message, defined as YANG's rpc input statement.\n"
108 " rpcreply - Reply to the RPC. The input data <file>s are expected in pairs - each RPC reply\n"
109 " input data <file> must be followed by the origin RPC input data <file> for the reply.\n"
110 " The same rule of pairing applies also in case of 'auto' TYPE and input data file\n"
111 " containing RPC reply.\n"
112 " notif - Notification instance (content of the <notification> element without <eventTime>.\n\n"
113 " -O FILE, --operational=FILE\n"
114 " - Optional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the FILE contains running\n"
115 " configuration datastore and state data (operational datastore) referenced from\n"
116 " the RPC/Notification. The same data apply to all input data <file>s. Note that\n"
117 " the file is validated as 'data' TYPE. Special value '!' can be used as FILE argument\n"
118 " to ignore the external references.\n\n"
119 " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n"
120 "Tree output specific options:\n"
121 " --tree-help - Print help on tree symbols and exit.\n"
122 " --tree-print-groupings\n"
123 " Print top-level groupings in a separate section.\n"
124 " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n"
125 " --tree-no-leafref-target\n"
126 " Do not print target nodes of leafrefs.\n"
127 " --tree-path=SCHEMA_PATH\n"
128 " Print only the specified subtree.\n"
129 " --tree-line-length=LINE_LENGTH\n"
130 " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n"
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800131 " can often appear).\n\n"
132 "Info output specific options:\n"
133 " -P INFOPATH, --info-path=INFOPATH\n"
134 " - Schema path with full module names used as node's prefixes, the path identify the root\n"
135 " node of the subtree to print information about.\n"
136 " --single-node - Print information about a single node instead of a subtree."
Radek Krejcied5acc52019-04-25 15:57:04 +0200137 "\n");
138}
139
140void
141tree_help(void)
142{
143 fprintf(stdout, "Each node is printed as:\n\n");
144 fprintf(stdout, "<status> <flags> <name> <opts> <type> <if-features>\n\n"
145 " <status> is one of:\n"
146 " + for current\n"
147 " x for deprecated\n"
148 " o for obsolete\n\n"
149 " <flags> is one of:\n"
150 " rw for configuration data\n"
151 " ro for status data\n"
152 " -x for RPCs\n"
153 " -n for Notification\n\n"
154 " <name> is the name of the node\n"
155 " (<name>) means that the node is a choice node\n"
156 " :(<name>) means that the node is a case node\n\n"
157 " if the node is augmented into the tree from another module,\n"
158 " it is printed with the module name as <module-name>:<name>.\n\n"
159 " <opts> is one of:\n"
160 " ? for an optional leaf or choice\n"
161 " ! for a presence container\n"
162 " * for a leaf-list or list\n"
163 " [<keys>] for a list's keys\n\n"
164 " <type> is the name of the type for leafs and leaf-lists\n"
165 " If there is a default value defined, it is printed within\n"
166 " angle brackets <default-value>.\n"
167 " If the type is a leafref, the type is printed as -> TARGET`\n\n"
168 " <if-features> is the list of features this node depends on,\n"
169 " printed within curly brackets and a question mark {...}?\n\n");
170}
171
172void
173version(void)
174{
175 fprintf(stdout, "yanglint %s\n", PROJECT_VERSION);
176}
177
178void
179libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
180{
181 char *levstr;
182
183 if (level <= verbose) {
184 switch(level) {
185 case LY_LLERR:
186 levstr = "err :";
187 break;
188 case LY_LLWRN:
189 levstr = "warn:";
190 break;
191 case LY_LLVRB:
192 levstr = "verb:";
193 break;
194 default:
195 levstr = "dbg :";
196 break;
197 }
198 if (path) {
199 fprintf(stderr, "%s %s (%s)\n", levstr, msg, path);
200 } else {
201 fprintf(stderr, "%s %s\n", levstr, msg);
202 }
203 }
204}
205
206/*
207 * return:
208 * 0 - error
209 * 1 - schema format
210 * 2 - data format
211 */
212static int
Radek Krejcie7b95092019-05-15 11:03:07 +0200213get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data)
Radek Krejcied5acc52019-04-25 15:57:04 +0200214{
215 char *ptr;
216 LYS_INFORMAT informat_s;
Radek Krejcied5acc52019-04-25 15:57:04 +0200217 LYD_FORMAT informat_d;
Radek Krejcie7b95092019-05-15 11:03:07 +0200218
Radek Krejcied5acc52019-04-25 15:57:04 +0200219 /* get the file format */
220 if ((ptr = strrchr(filename, '.')) != NULL) {
221 ++ptr;
222 if (!strcmp(ptr, "yang")) {
223 informat_s = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200224 informat_d = 0;
225 } else if (!strcmp(ptr, "yin")) {
226 informat_s = LYS_IN_YIN;
227 informat_d = 0;
228 } else if (!strcmp(ptr, "xml")) {
229 informat_s = 0;
230 informat_d = LYD_XML;
231 } else if (!strcmp(ptr, "json")) {
232 informat_s = 0;
233 informat_d = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200234 } else {
235 fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr);
236 return 0;
237 }
238 } else {
239 fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
240 return 0;
241 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200242
Radek Krejcied5acc52019-04-25 15:57:04 +0200243 if (data) {
244 (*data) = informat_d;
245 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200246
Radek Krejcied5acc52019-04-25 15:57:04 +0200247 if (schema) {
248 (*schema) = informat_s;
249 }
250
251 if (informat_s) {
252 return 1;
253 } else {
254 return 2;
255 }
256}
257
258int
259main_ni(int argc, char* argv[])
260{
261 int ret = EXIT_FAILURE;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800262 int opt, opt_index = 0, i, featsize = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200263 struct option options[] = {
264#if 0
265 {"auto", no_argument, NULL, 'a'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200266#endif
Radek Krejciff61c882020-09-03 13:04:18 +0200267 {"default", required_argument, NULL, 'd'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200268 {"format", required_argument, NULL, 'f'},
269 {"features", required_argument, NULL, 'F'},
270#if 0
271 {"tree-print-groupings", no_argument, NULL, 'g'},
272 {"tree-print-uses", no_argument, NULL, 'u'},
273 {"tree-no-leafref-target", no_argument, NULL, 'n'},
274 {"tree-path", required_argument, NULL, 'P'},
275 {"tree-line-length", required_argument, NULL, 'L'},
276#endif
277 {"help", no_argument, NULL, 'h'},
278#if 0
279 {"tree-help", no_argument, NULL, 'H'},
280#endif
281 {"allimplemented", no_argument, NULL, 'i'},
282 {"disable-cwd-search", no_argument, NULL, 'D'},
283 {"list", no_argument, NULL, 'l'},
284#if 0
285 {"merge", no_argument, NULL, 'm'},
286#endif
287 {"output", required_argument, NULL, 'o'},
288 {"path", required_argument, NULL, 'p'},
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800289 {"info-path", required_argument, NULL, 'P'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200290#if 0
291 {"running", required_argument, NULL, 'r'},
292 {"operational", required_argument, NULL, 'O'},
Radek Krejcie7b95092019-05-15 11:03:07 +0200293#endif
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800294 {"single-node", no_argument, NULL, 'q'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200295 {"strict", no_argument, NULL, 's'},
296 {"type", required_argument, NULL, 't'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200297 {"version", no_argument, NULL, 'v'},
298 {"verbose", no_argument, NULL, 'V'},
299#ifndef NDEBUG
300 {"debug", required_argument, NULL, 'G'},
301#endif
302 {NULL, required_argument, NULL, 'y'},
303 {NULL, 0, NULL, 0}
304 };
Radek Krejci241f6b52020-05-21 18:13:49 +0200305 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200306 struct ly_ctx *ctx = NULL;
307 const struct lys_module *mod;
308 LYS_OUTFORMAT outformat_s = 0;
309 LYS_INFORMAT informat_s;
Radek Krejcie7b95092019-05-15 11:03:07 +0200310 LYD_FORMAT informat_d, outformat_d = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200311#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200312 LYD_FORMAT ylformat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200313#endif
314 struct ly_set *searchpaths = NULL;
315 const char *outtarget_s = NULL;
316 char **feat = NULL, *ptr, *featlist, *dir;
317 struct stat st;
318 uint32_t u;
Michal Vasko25d6ad02020-10-22 12:20:22 +0200319 int options_ctx = LY_CTX_NO_YANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 int autodetection = 0, options_parser = 0, merge = 0;
321 const char *oper_file = NULL;
Radek Krejciff61c882020-09-03 13:04:18 +0200322 int options_dflt = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200323#if 0
Radek Krejciff61c882020-09-03 13:04:18 +0200324 ly_bool envelope = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200325 const char *envelope_s = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200326 char *ylpath = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200327 struct lyxml_elem *iter, *elem;
328 struct *subroot, *next, *node;
329#endif
Michal Vaskof03ed032020-03-04 13:31:44 +0100330 struct lyd_node *tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200331 struct dataitem {
332 const char *filename;
Radek Krejcied5acc52019-04-25 15:57:04 +0200333 struct lyd_node *tree;
334 struct dataitem *next;
335 LYD_FORMAT format;
Michal Vaskoa3881362020-01-21 15:57:35 +0100336 int flags;
Radek Krejcied5acc52019-04-25 15:57:04 +0200337 } *data = NULL, *data_item, *data_prev = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200338 struct ly_set *mods = NULL;
339 void *p;
Radek Krejcied5acc52019-04-25 15:57:04 +0200340
341 opterr = 0;
342#ifndef NDEBUG
Radek Krejci733811f2020-09-04 10:15:10 +0200343 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 +0200344#else
Radek Krejci733811f2020-09-04 10:15:10 +0200345 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 +0200346#endif
347 {
348 switch (opt) {
349#if 0
Radek Krejciff61c882020-09-03 13:04:18 +0200350 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200351 case 'a':
352 envelope = 1;
353 break;
Radek Krejciff61c882020-09-03 13:04:18 +0200354#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200355 case 'd':
356 if (!strcmp(optarg, "all")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200357 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200358 } else if (!strcmp(optarg, "all-tagged")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200359 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200360 } else if (!strcmp(optarg, "trim")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200361 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
Radek Krejcied5acc52019-04-25 15:57:04 +0200362 } else if (!strcmp(optarg, "implicit-tagged")) {
Radek Krejciff61c882020-09-03 13:04:18 +0200363 options_dflt = (options_dflt & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200364 } else {
365 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
366 help(1);
367 goto cleanup;
368 }
369 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200370 case 'f':
371 if (!strcasecmp(optarg, "yang")) {
372 outformat_s = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200373 outformat_d = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200374#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200375 } else if (!strcasecmp(optarg, "tree")) {
376 outformat_s = LYS_OUT_TREE;
377 outformat_d = 0;
378 } else if (!strcasecmp(optarg, "tree-rfc")) {
379 outformat_s = LYS_OUT_TREE;
380 outoptions_s |= LYS_OUTOPT_TREE_RFC;
381 outformat_d = 0;
FredGand944bdc2019-11-05 21:57:07 +0800382#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200383 } else if (!strcasecmp(optarg, "yin")) {
384 outformat_s = LYS_OUT_YIN;
385 outformat_d = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800386 } else if (!strcasecmp(optarg, "info")) {
387 outformat_s = LYS_OUT_YANG_COMPILED;
Radek Krejcied5acc52019-04-25 15:57:04 +0200388 outformat_d = 0;
389 } else if (!strcasecmp(optarg, "xml")) {
390 outformat_s = 0;
391 outformat_d = LYD_XML;
392 } else if (!strcasecmp(optarg, "json")) {
393 outformat_s = 0;
394 outformat_d = LYD_JSON;
Radek Krejcied5acc52019-04-25 15:57:04 +0200395 } 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;
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800432#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200433 case 'P':
434 outtarget_s = optarg;
435 break;
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800436#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200437 case 'L':
438 outline_length_s = atoi(optarg);
439 break;
440#endif
441 case 'h':
442 help(0);
443 ret = EXIT_SUCCESS;
444 goto cleanup;
445#if 0
446 case 'H':
447 tree_help();
448 ret = EXIT_SUCCESS;
449 goto cleanup;
450#endif
451 case 'i':
Michal Vasko25d6ad02020-10-22 12:20:22 +0200452 options_ctx |= LY_CTX_ALL_IMPLEMENTED;
Radek Krejcied5acc52019-04-25 15:57:04 +0200453 break;
454 case 'D':
455 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
456 fprintf(stderr, "yanglint error: -D specified too many times.\n");
457 goto cleanup;
458 } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
459 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
460 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
461 } else {
462 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
463 }
464 break;
465 case 'l':
466 list = 1;
467 break;
468#if 0
469 case 'm':
470 merge = 1;
471 break;
472#endif
473 case 'o':
Radek Krejcia5bba312020-01-09 15:41:20 +0100474 if (out) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200475 if (ly_out_filepath(out, optarg) != NULL) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100476 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
477 goto cleanup;
478 }
479 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200480 if (ly_out_new_filepath(optarg, &out)) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100481 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
482 goto cleanup;
483 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200484 }
485 break;
486 case 'p':
487 if (stat(optarg, &st) == -1) {
488 fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
489 goto cleanup;
490 }
491 if (!S_ISDIR(st.st_mode)) {
492 fprintf(stderr, "yanglint error: Provided search path is not a directory.\n");
493 goto cleanup;
494 }
495 if (!searchpaths) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200496 if (ly_set_new(&searchpaths)) {
497 fprintf(stderr, "yanglint error: Preparing storage for searchpaths failed.\n");
498 goto cleanup;
499 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200500 }
Radek Krejciba03a5a2020-08-27 14:40:41 +0200501 if (ly_set_add(searchpaths, optarg, 0, NULL)) {
502 fprintf(stderr, "yanglint error: Storing searchpath failed.\n");
503 goto cleanup;
504 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200505 break;
506#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200507 case 'O':
508 if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) {
509 fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n");
510 goto cleanup;
511 }
512 if (optarg[0] == '!') {
513 /* ignore extenral dependencies to the operational datastore */
514 options_parser |= LYD_OPT_NOEXTDEPS;
515 } else {
516 /* external file with the operational datastore */
517 oper_file = optarg;
518 }
519 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200520#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200521 case 's':
Radek Krejci7931b192020-06-25 17:05:03 +0200522 options_parser |= LYD_PARSE_STRICT;
Radek Krejcied5acc52019-04-25 15:57:04 +0200523 break;
524 case 't':
525 if (!strcmp(optarg, "auto")) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200526 autodetection = 1;
Michal Vaskob36053d2020-03-26 15:49:30 +0100527 /*} else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100528 options_parser |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200529 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100530 options_parser |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200531 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100532 options_parser |= LYD_OPT_GETCONFIG;
Michal Vaskob36053d2020-03-26 15:49:30 +0100533 } else if (!strcmp(optarg, "edit")) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100534 options_parser |= LYD_OPT_EDIT;*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200535 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100536 /* no options */
Radek Krejcied5acc52019-04-25 15:57:04 +0200537 } else {
538 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
539 help(1);
540 goto cleanup;
541 }
542 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200543 case 'v':
544 version();
545 ret = EXIT_SUCCESS;
546 goto cleanup;
547 case 'V':
548 verbose++;
549 break;
550#ifndef NDEBUG
551 case 'G':
552 u = 0;
553 ptr = optarg;
554 while (ptr[0]) {
555 if (!strncmp(ptr, "dict", 4)) {
556 u |= LY_LDGDICT;
557 ptr += 4;
558 } else if (!strncmp(ptr, "yang", 4)) {
559 u |= LY_LDGYANG;
560 ptr += 4;
561 } else if (!strncmp(ptr, "yin", 3)) {
562 u |= LY_LDGYIN;
563 ptr += 3;
564 } else if (!strncmp(ptr, "xpath", 5)) {
565 u |= LY_LDGXPATH;
566 ptr += 5;
567 } else if (!strncmp(ptr, "diff", 4)) {
568 u |= LY_LDGDIFF;
569 ptr += 4;
570 }
571
572 if (ptr[0]) {
573 if (ptr[0] != ',') {
574 fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg);
575 goto cleanup;
576 }
577 ++ptr;
578 }
579 }
Radek Krejci68433c92020-10-12 17:03:55 +0200580 ly_log_dbg_groups(u);
Radek Krejcied5acc52019-04-25 15:57:04 +0200581 break;
582#endif
583#if 0
584 case 'y':
585 ptr = strrchr(optarg, '.');
586 if (ptr) {
587 ptr++;
588 if (!strcmp(ptr, "xml")) {
589 ylformat = LYD_XML;
590 } else if (!strcmp(ptr, "json")) {
591 ylformat = LYD_JSON;
592 } else {
593 fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr);
594 goto cleanup;
595 }
596 } else {
597 fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n");
598 goto cleanup;
599 }
600 ylpath = optarg;
601 break;
602#endif
603 default:
604 help(1);
605 if (optopt) {
606 fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt);
607 } else {
608 fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]);
609 }
610 goto cleanup;
611 }
612 }
613
614 /* check options compatibility */
615 if (!list && optind >= argc) {
616 help(1);
617 fprintf(stderr, "yanglint error: missing <file> to process\n");
618 goto cleanup;
619 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200620 if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) {
621 /* we have multiple schemas to be printed as YIN or YANG */
622 fprintf(stderr, "yanglint error: too many schemas to convert and store.\n");
623 goto cleanup;
624 }
625 if (outoptions_s || outtarget_s || outline_length_s) {
626#if 0
627 if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) {
628 /* we have --tree-print-grouping with other output format than tree */
629 fprintf(stderr,
630 "yanglint warning: --tree options take effect only in case of the tree output format.\n");
631 }
632 }
633 if (merge) {
634 if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) {
635 fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n");
636 merge = 0;
637 } else {
638 /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged
639 * and the result will be validated */
640 options_parser |= LYD_OPT_TRUSTED;
641 }
642#endif
643 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200644 if (!outformat_d && options_dflt) {
645 /* we have options for printing default nodes, but data output not specified */
646 fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n");
647 }
Radek Krejciff61c882020-09-03 13:04:18 +0200648#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200649 if (outformat_s && (options_parser || autodetection)) {
650 /* we have options for printing data tree, but output is schema */
651 fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n");
652 }
653 if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) {
654 fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n");
655 /* ignore operational datastore file */
656 oper_file = NULL;
657 }
658 if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) {
659 /* add option to ignore ietf-yang-library data for implicit data type */
660 options_parser |= LYD_OPT_DATA_NO_YANGLIB;
661 }
Michal Vaskoa3881362020-01-21 15:57:35 +0100662#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200663
664 /* set callback for printing libyang messages */
665 ly_set_log_clb(libyang_verbclb, 1);
666#if 0
667 /* create libyang context */
668 if (ylpath) {
669 ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx);
670 } else {
671#else
672 {
673#endif
674 ly_ctx_new(NULL, options_ctx, &ctx);
675 }
676 if (!ctx) {
677 goto cleanup;
678 }
679
680 /* set searchpaths */
681 if (searchpaths) {
682 for (u = 0; u < searchpaths->count; u++) {
683 ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]);
684 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200685 }
686
687 /* derefered setting of verbosity in libyang after context initiation */
Radek Krejci52b6d512020-10-12 12:33:17 +0200688 ly_log_level(verbose);
Radek Krejcied5acc52019-04-25 15:57:04 +0200689
Radek Krejciba03a5a2020-08-27 14:40:41 +0200690 if (ly_set_new(&mods)) {
691 fprintf(stderr, "yanglint error: Preparing storage for the parsed modules failed.\n");
692 goto cleanup;
693 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200694
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 */
Radek Krejci83126492020-08-15 15:40:04 +0200705 int unset_path = 1;
706
Radek Krejcied5acc52019-04-25 15:57:04 +0200707 if (verbose >= 2) {
708 fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]);
709 }
Radek Krejci83126492020-08-15 15:40:04 +0200710
711 /* add temporarily also the path of the module itself */
Radek Krejcied5acc52019-04-25 15:57:04 +0200712 dir = strdup(argv[optind + i]);
Radek Krejci83126492020-08-15 15:40:04 +0200713 if (ly_ctx_set_searchdir(ctx, ptr = dirname(dir)) == LY_EEXIST) {
714 unset_path = 0;
715 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200716 lys_parse_path(ctx, argv[optind + i], informat_s, &mod);
Radek Krejcie58f97f2020-08-18 11:45:08 +0200717 ly_ctx_unset_searchdir_last(ctx, unset_path);
Radek Krejcied5acc52019-04-25 15:57:04 +0200718 free(dir);
719 if (!mod) {
720 goto cleanup;
721 }
Radek Krejciba03a5a2020-08-27 14:40:41 +0200722 if (ly_set_add(mods, (void *)mod, 0, NULL)) {
723 fprintf(stderr, "yanglint error: Storing parsed module for further processing failed.\n");
724 goto cleanup;
725 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200726 } else {
727 if (autodetection && informat_d != LYD_XML) {
728 /* data file content autodetection is possible only for XML input */
729 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
730 goto cleanup;
731 }
732
733 /* remember data filename and its format */
734 if (!data) {
735 data = data_item = malloc(sizeof *data);
736 } else {
737 for (data_item = data; data_item->next; data_item = data_item->next);
738 data_item->next = malloc(sizeof *data_item);
739 data_item = data_item->next;
740 }
741 data_item->filename = argv[optind + i];
742 data_item->format = informat_d;
Michal Vaskoa3881362020-01-21 15:57:35 +0100743 data_item->flags = options_parser;
Radek Krejcied5acc52019-04-25 15:57:04 +0200744 data_item->tree = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200745 data_item->next = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200746 }
747 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200748 if (outformat_d && !data && !list) {
749 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
750 goto cleanup;
751 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200752
753 /* enable specified features, if not specified, all the module's features are enabled */
754 u = 4; /* skip internal libyang modules */
755 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci3d46f612020-08-13 12:01:53 +0200756 if (!mod->implemented) {
757 continue;
758 }
759
Radek Krejcied5acc52019-04-25 15:57:04 +0200760 for (i = 0; i < featsize; i++) {
761 if (!strcmp(feat[i], mod->name)) {
762 /* parse features spec */
763 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
764 ptr = NULL;
765 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
766 if (verbose >= 2) {
767 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
768 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100769 /*if (lys_feature_enable(mod, ptr)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200770 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100771 }*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200772 }
773 free(featlist);
774 break;
775 }
776 }
777 if (i == featsize) {
778 if (verbose >= 2) {
779 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
780 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100781 //lys_feature_enable(mod, "*");
Radek Krejcied5acc52019-04-25 15:57:04 +0200782 }
783 }
Radek Krejci460937d2020-09-04 10:15:36 +0200784 if (!out && (outformat_s || data)) {
785 ly_out_new_file(stdout, &out);
786 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200787 /* convert (print) to FORMAT */
788 if (outformat_s) {
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800789 if (outtarget_s) {
Radek Krejcibc5644c2020-10-27 14:53:17 +0100790 const struct lysc_node *node = lys_find_path(ctx, NULL, outtarget_s, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100791 if (node) {
792 lys_print_node(out, node, outformat_s, outline_length_s, outoptions_s);
793 } else {
794 fprintf(stderr, "yanglint error: The requested schema node \"%s\" does not exists.\n", outtarget_s);
795 }
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800796 } else {
797 for (u = 0; u < mods->count; u++) {
798 if (u) {
Radek Krejci241f6b52020-05-21 18:13:49 +0200799 ly_print(out, "\n");
Radek Krejcie1f6d5a2019-11-17 14:03:04 +0800800 }
Michal Vasko7c8439f2020-08-05 13:25:19 +0200801 lys_print_module(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
Radek Krejcied5acc52019-04-25 15:57:04 +0200802 }
803 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200804 } else if (data) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200805
806 /* prepare operational datastore when specified for RPC/Notification */
807 if (oper_file) {
Radek Krejci7931b192020-06-25 17:05:03 +0200808 struct ly_in *in;
809 tree = NULL;
810
811 if (ly_in_new_filepath(oper_file, 0, &in)) {
812 fprintf(stderr, "yanglint error: Unable to open an operational data file \"%s\".\n", oper_file);
Radek Krejcied5acc52019-04-25 15:57:04 +0200813 goto cleanup;
814 }
Radek Krejci7931b192020-06-25 17:05:03 +0200815 if (lyd_parse_data(ctx, in, 0, LYD_PARSE_ONLY, 0, &tree) || !tree) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200816 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
Radek Krejci7931b192020-06-25 17:05:03 +0200817 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200818 goto cleanup;
819 }
Radek Krejci7931b192020-06-25 17:05:03 +0200820 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200821 }
822
823 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
824 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
Radek Krejcie7b95092019-05-15 11:03:07 +0200825#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200826 if (autodetection) {
827 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
828 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
829 /* automatically detect data type from the data top level */
830 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
831 if (!data_item->xml) {
832 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
833 goto cleanup;
834 }
835
836 /* NOTE: namespace is ignored to simplify usage of this feature */
837 if (!strcmp(data_item->xml->name, "data")) {
838 if (verbose >= 2) {
839 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
840 }
841 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
842 data_item->type = LYD_OPT_DATA;
843 } else if (!strcmp(data_item->xml->name, "config")) {
844 if (verbose >= 2) {
845 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
846 }
847 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
848 data_item->type = LYD_OPT_CONFIG;
849 } else if (!strcmp(data_item->xml->name, "get-reply")) {
850 if (verbose >= 2) {
851 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
852 }
853 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
854 data_item->type = LYD_OPT_GET;
855 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
856 if (verbose >= 2) {
857 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
858 }
859 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
860 data_item->type = LYD_OPT_GETCONFIG;
861 } else if (!strcmp(data_item->xml->name, "edit-config")) {
862 if (verbose >= 2) {
863 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
864 }
865 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
866 data_item->type = LYD_OPT_EDIT;
867 } else if (!strcmp(data_item->xml->name, "rpc")) {
868 if (verbose >= 2) {
869 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
870 }
871 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
872 data_item->type = LYD_OPT_RPC;
873 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
874 if (verbose >= 2) {
875 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
876 }
877
878 data_item->type = LYD_OPT_RPCREPLY;
879 if (!data_item->next || (data_prev && !data_prev->tree)) {
880 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
881 goto cleanup;
882 }
883
884 continue;
885 } else if (!strcmp(data_item->xml->name, "notification")) {
886 if (verbose >= 2) {
887 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
888 }
889 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
890 data_item->type = LYD_OPT_NOTIF;
891
892 /* ignore eventTime element if present */
893 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
894 lyxml_free(ctx, data_item->xml->child);
895 }
896 } else {
897 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
898 data_item->xml->name);
899 goto cleanup;
900 }
901
902 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
903 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
904parse_reply:
905 /* check result of the RPC parsing, we are going to do another parsing in this step */
906 if (ly_errno) {
907 goto cleanup;
908 }
909
910 /* check that we really have RPC for the reply */
911 if (data_item->type != LYD_OPT_RPC) {
912 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
913 goto cleanup;
914 }
915
916 if (data_prev->format == LYD_XML) {
917 /* ignore <ok> and <rpc-error> elements if present */
918 u = 0;
919 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
920 if (!strcmp(data_prev->xml->child->name, "ok")) {
921 if (u) {
922 /* rpc-error or ok already present */
923 u = 0x8; /* error flag */
924 } else {
925 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
926 }
927 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
928 if (u && (u & 0x1)) {
929 /* ok already present, rpc-error can be present multiple times */
930 u = 0x8; /* error flag */
931 } else {
932 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
933 }
934 }
935
936 if (u == 0x8) {
937 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
938 goto cleanup;
939 } else if (u & 0x4) {
940 lyxml_free(ctx, data_prev->xml->child);
941 u &= ~0x4; /* unset lyxml_free() flag */
942 }
943 }
944
945 /* finally, parse RPC reply from the previous step */
946 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
947 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
948 } else { /* LYD_JSON */
949 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
950 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
951 }
952 }
953 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
954 if (data_prev && !data_prev->tree) {
955 /* now we should have RPC for the preceding RPC reply */
956 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
957 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
958 data_item->type = LYD_OPT_RPC;
959 goto parse_reply;
960 } else {
961 /* now we have RPC reply which will be parsed in next step together with its RPC */
962 if (!data_item->next) {
963 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
964 goto cleanup;
965 }
966 if (data_item->format == LYD_XML) {
967 /* create rpc-reply container to unify handling with autodetection */
968 data_item->xml = calloc(1, sizeof *data_item->xml);
969 if (!data_item->xml) {
970 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
971 goto cleanup;
972 }
973 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
974 data_item->xml->prev = data_item->xml;
975 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
976 if (data_item->xml->child) {
977 data_item->xml->child->parent = data_item->xml;
978 }
979 }
980 continue;
981 }
982 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200983#else
984 {
985#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200986 /* TODO optimize use of ly_in in the loop */
987 struct ly_in *in;
988 if (ly_in_new_filepath(data_item->filename, 0, &in)) {
989 fprintf(stderr, "yanglint error: input data file \"%s\".\n", data_item->filename);
990 goto cleanup;
991 }
Radek Krejci5536d282020-08-04 23:27:44 +0200992 if (lyd_parse_data(ctx, in, 0, options_parser, LYD_VALIDATE_PRESENT, &data_item->tree)) {
Radek Krejci7931b192020-06-25 17:05:03 +0200993 fprintf(stderr, "yanglint error: Failed to parse input data file \"%s\".\n", data_item->filename);
994 ly_in_free(in, 0);
995 goto cleanup;
996 }
997 ly_in_free(in, 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200998 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200999#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001000 if (merge && data != data_item) {
1001 if (!data->tree) {
1002 data->tree = data_item->tree;
1003 } else if (data_item->tree) {
1004 /* merge results */
1005 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
1006 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
1007 goto cleanup;
1008 }
1009 }
1010 data_item->tree = NULL;
1011 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001012#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001013 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001014#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001015 if (merge) {
1016 /* validate the merged data tree, do not trust the input, invalidate all the data first */
1017 LY_TREE_FOR(data->tree, subroot) {
1018 LY_TREE_DFS_BEGIN(subroot, next, node) {
1019 node->validity = LYD_VAL_OK;
1020 switch (node->schema->nodetype) {
1021 case LYS_LEAFLIST:
1022 case LYS_LEAF:
1023 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1024 node->validity |= LYD_VAL_LEAFREF;
1025 }
1026 break;
1027 case LYS_LIST:
1028 node->validity |= LYD_VAL_UNIQUE;
1029 /* falls through */
1030 case LYS_CONTAINER:
1031 case LYS_NOTIF:
1032 case LYS_RPC:
1033 case LYS_ACTION:
1034 node->validity |= LYD_VAL_MAND;
1035 break;
1036 default:
1037 break;
1038 }
1039 LY_TREE_DFS_END(subroot, next, node)
1040 }
1041 }
1042 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1043 goto cleanup;
1044 }
1045 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001046#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001047 /* print only if data output format specified */
1048 if (outformat_d) {
1049 for (data_item = data; data_item; data_item = data_item->next) {
1050 if (!merge && verbose >= 2) {
1051 fprintf(stdout, "File %s:\n", data_item->filename);
1052 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001053#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001054 if (outformat_d == LYD_XML && envelope) {
1055 switch (data_item->type) {
1056 case LYD_OPT_DATA:
1057 envelope_s = "data";
1058 break;
1059 case LYD_OPT_CONFIG:
1060 envelope_s = "config";
1061 break;
1062 case LYD_OPT_GET:
1063 envelope_s = "get-reply";
1064 break;
1065 case LYD_OPT_GETCONFIG:
1066 envelope_s = "get-config-reply";
1067 break;
1068 case LYD_OPT_EDIT:
1069 envelope_s = "edit-config";
1070 break;
1071 case LYD_OPT_RPC:
1072 envelope_s = "rpc";
1073 break;
1074 case LYD_OPT_RPCREPLY:
1075 envelope_s = "rpc-reply";
1076 break;
1077 case LYD_OPT_NOTIF:
1078 envelope_s = "notification";
1079 break;
1080 }
1081 fprintf(out, "<%s>\n", envelope_s);
1082 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1083 /* action */
1084 fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n");
1085 }
1086 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001087#endif
Radek Krejciff61c882020-09-03 13:04:18 +02001088 lyd_print_all(out, data_item->tree, outformat_d, options_dflt);
Radek Krejcie7b95092019-05-15 11:03:07 +02001089#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001090 if (envelope_s) {
1091 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1092 fprintf(out, "</action>\n");
1093 }
1094 fprintf(out, "</%s>\n", envelope_s);
1095 }
1096 if (merge) {
1097 /* stop after first item */
1098 break;
1099 }
Radek Krejcie7b95092019-05-15 11:03:07 +02001100#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001101 }
1102 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001103 }
1104#if 0
1105 if (list) {
1106 print_list(out, ctx, outformat_d);
1107 }
1108#endif
1109
1110 ret = EXIT_SUCCESS;
1111
1112cleanup:
Radek Krejcied5acc52019-04-25 15:57:04 +02001113 ly_set_free(mods, NULL);
1114 ly_set_free(searchpaths, NULL);
1115 for (i = 0; i < featsize; i++) {
1116 free(feat[i]);
1117 }
1118 free(feat);
Radek Krejcied5acc52019-04-25 15:57:04 +02001119 for (; data; data = data_item) {
1120 data_item = data->next;
Radek Krejcie7b95092019-05-15 11:03:07 +02001121 lyd_free_all(data->tree);
Radek Krejcied5acc52019-04-25 15:57:04 +02001122 free(data);
1123 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001124 ly_ctx_destroy(ctx, NULL);
1125
Radek Krejci241f6b52020-05-21 18:13:49 +02001126 ly_out_free(out, NULL, 1);
Radek Krejcied5acc52019-04-25 15:57:04 +02001127 return ret;
1128}