blob: 220ef33df6e3141468a64904d7baeb99e787975b [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
205get_fileformat(const char *filename, LYS_INFORMAT *schema/* TODO , LYD_FORMAT *data */)
206{
207 char *ptr;
208 LYS_INFORMAT informat_s;
209#if 0
210 LYD_FORMAT informat_d;
211#endif
212 /* get the file format */
213 if ((ptr = strrchr(filename, '.')) != NULL) {
214 ++ptr;
215 if (!strcmp(ptr, "yang")) {
216 informat_s = LYS_IN_YANG;
217#if 0
218 informat_d = 0;
219 } else if (!strcmp(ptr, "yin")) {
220 informat_s = LYS_IN_YIN;
221 informat_d = 0;
222 } else if (!strcmp(ptr, "xml")) {
223 informat_s = 0;
224 informat_d = LYD_XML;
225 } else if (!strcmp(ptr, "json")) {
226 informat_s = 0;
227 informat_d = LYD_JSON;
228#endif
229 } else {
230 fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr);
231 return 0;
232 }
233 } else {
234 fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename);
235 return 0;
236 }
237#if 0
238 if (data) {
239 (*data) = informat_d;
240 }
241#endif
242 if (schema) {
243 (*schema) = informat_s;
244 }
245
246 if (informat_s) {
247 return 1;
248 } else {
249 return 2;
250 }
251}
252
253int
254main_ni(int argc, char* argv[])
255{
256 int ret = EXIT_FAILURE;
Radek Krejci693262f2019-04-29 15:23:20 +0200257 int opt, opt_index = 0, i, featsize = 0, compiled = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200258 struct option options[] = {
259#if 0
260 {"auto", no_argument, NULL, 'a'},
261 {"default", required_argument, NULL, 'd'},
262#endif
263 {"format", required_argument, NULL, 'f'},
264 {"features", required_argument, NULL, 'F'},
265#if 0
266 {"tree-print-groupings", no_argument, NULL, 'g'},
267 {"tree-print-uses", no_argument, NULL, 'u'},
268 {"tree-no-leafref-target", no_argument, NULL, 'n'},
269 {"tree-path", required_argument, NULL, 'P'},
270 {"tree-line-length", required_argument, NULL, 'L'},
271#endif
Radek Krejci693262f2019-04-29 15:23:20 +0200272 {"compiled", no_argument, NULL, 'c'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200273 {"help", no_argument, NULL, 'h'},
274#if 0
275 {"tree-help", no_argument, NULL, 'H'},
276#endif
277 {"allimplemented", no_argument, NULL, 'i'},
278 {"disable-cwd-search", no_argument, NULL, 'D'},
279 {"list", no_argument, NULL, 'l'},
280#if 0
281 {"merge", no_argument, NULL, 'm'},
282#endif
283 {"output", required_argument, NULL, 'o'},
284 {"path", required_argument, NULL, 'p'},
285#if 0
286 {"running", required_argument, NULL, 'r'},
287 {"operational", required_argument, NULL, 'O'},
288 {"strict", no_argument, NULL, 's'},
289 {"type", required_argument, NULL, 't'},
290#endif
291 {"version", no_argument, NULL, 'v'},
292 {"verbose", no_argument, NULL, 'V'},
293#ifndef NDEBUG
294 {"debug", required_argument, NULL, 'G'},
295#endif
296 {NULL, required_argument, NULL, 'y'},
297 {NULL, 0, NULL, 0}
298 };
299 FILE *out = stdout;
300 struct ly_ctx *ctx = NULL;
301 const struct lys_module *mod;
302 LYS_OUTFORMAT outformat_s = 0;
303 LYS_INFORMAT informat_s;
304#if 0
305 LYD_FORMAT informat_d, outformat_d = 0, ylformat = 0;
306#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;
313#if 0
314 const char *oper_file = NULL, *envelope_s = NULL;
315 char *ylpath = NULL;
316 int options_dflt = 0, options_parser = 0, envelope = 0, autodetection = 0, merge = 0;
317 struct dataitem {
318 const char *filename;
319 struct lyxml_elem *xml;
320 struct lyd_node *tree;
321 struct dataitem *next;
322 LYD_FORMAT format;
323 int type;
324 } *data = NULL, *data_item, *data_prev = NULL;
325 struct lyxml_elem *iter, *elem;
326 struct lyd_node *oper = NULL, *subroot, *next, *node;
327#endif
328 struct ly_set *mods = NULL;
329 void *p;
330 int index = 0;
331
332 opterr = 0;
333#ifndef NDEBUG
Radek Krejci693262f2019-04-29 15:23:20 +0200334 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 +0200335#else
Radek Krejci693262f2019-04-29 15:23:20 +0200336 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 +0200337#endif
338 {
339 switch (opt) {
340#if 0
341 case 'a':
342 envelope = 1;
343 break;
344 case 'd':
345 if (!strcmp(optarg, "all")) {
346 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL;
347 } else if (!strcmp(optarg, "all-tagged")) {
348 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
349 } else if (!strcmp(optarg, "trim")) {
350 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM;
351 } else if (!strcmp(optarg, "implicit-tagged")) {
352 options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
353 } else {
354 fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg);
355 help(1);
356 goto cleanup;
357 }
358 break;
359#endif
Radek Krejci693262f2019-04-29 15:23:20 +0200360 case 'c':
361 compiled = 1;
362 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200363 case 'f':
364 if (!strcasecmp(optarg, "yang")) {
365 outformat_s = LYS_OUT_YANG;
366#if 0
367 outformat_d = 0;
368 } else if (!strcasecmp(optarg, "tree")) {
369 outformat_s = LYS_OUT_TREE;
370 outformat_d = 0;
371 } else if (!strcasecmp(optarg, "tree-rfc")) {
372 outformat_s = LYS_OUT_TREE;
373 outoptions_s |= LYS_OUTOPT_TREE_RFC;
374 outformat_d = 0;
375 } else if (!strcasecmp(optarg, "yin")) {
376 outformat_s = LYS_OUT_YIN;
377 outformat_d = 0;
378 } else if (!strcasecmp(optarg, "jsons")) {
379 outformat_s = LYS_OUT_JSON;
380 outformat_d = 0;
381 } else if (!strcasecmp(optarg, "xml")) {
382 outformat_s = 0;
383 outformat_d = LYD_XML;
384 } else if (!strcasecmp(optarg, "json")) {
385 outformat_s = 0;
386 outformat_d = LYD_JSON;
387#endif
388 } else {
389 fprintf(stderr, "yanglint error: unknown output format %s\n", optarg);
390 help(1);
391 goto cleanup;
392 }
393 break;
394 case 'F':
395 featsize++;
396 if (!feat) {
397 p = malloc(sizeof *feat);
398 } else {
399 p = realloc(feat, featsize * sizeof *feat);
400 }
401 if (!p) {
402 fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
403 goto cleanup;
404 }
405 feat = p;
406 feat[featsize - 1] = strdup(optarg);
407 ptr = strchr(feat[featsize - 1], ':');
408 if (!ptr) {
409 fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg);
410 goto cleanup;
411 }
412 *ptr = '\0';
413
414 break;
415#if 0
416 case 'g':
417 outoptions_s |= LYS_OUTOPT_TREE_GROUPING;
418 break;
419 case 'u':
420 outoptions_s |= LYS_OUTOPT_TREE_USES;
421 break;
422 case 'n':
423 outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF;
424 break;
425 case 'P':
426 outtarget_s = optarg;
427 break;
428 case 'L':
429 outline_length_s = atoi(optarg);
430 break;
431#endif
432 case 'h':
433 help(0);
434 ret = EXIT_SUCCESS;
435 goto cleanup;
436#if 0
437 case 'H':
438 tree_help();
439 ret = EXIT_SUCCESS;
440 goto cleanup;
441#endif
442 case 'i':
443 options_ctx |= LY_CTX_ALLIMPLEMENTED;
444 break;
445 case 'D':
446 if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) {
447 fprintf(stderr, "yanglint error: -D specified too many times.\n");
448 goto cleanup;
449 } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) {
450 options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
451 options_ctx |= LY_CTX_DISABLE_SEARCHDIRS;
452 } else {
453 options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD;
454 }
455 break;
456 case 'l':
457 list = 1;
458 break;
459#if 0
460 case 'm':
461 merge = 1;
462 break;
463#endif
464 case 'o':
465 if (out != stdout) {
466 fclose(out);
467 }
468 out = fopen(optarg, "w");
469 if (!out) {
470 fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno));
471 goto cleanup;
472 }
473 break;
474 case 'p':
475 if (stat(optarg, &st) == -1) {
476 fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
477 goto cleanup;
478 }
479 if (!S_ISDIR(st.st_mode)) {
480 fprintf(stderr, "yanglint error: Provided search path is not a directory.\n");
481 goto cleanup;
482 }
483 if (!searchpaths) {
484 searchpaths = ly_set_new();
485 }
486 ly_set_add(searchpaths, optarg, 0);
487 break;
488#if 0
489 case 'r':
490 case 'O':
491 if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) {
492 fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n");
493 goto cleanup;
494 }
495 if (optarg[0] == '!') {
496 /* ignore extenral dependencies to the operational datastore */
497 options_parser |= LYD_OPT_NOEXTDEPS;
498 } else {
499 /* external file with the operational datastore */
500 oper_file = optarg;
501 }
502 break;
503 case 's':
504 options_parser |= LYD_OPT_STRICT;
505 break;
506 case 't':
507 if (!strcmp(optarg, "auto")) {
508 options_parser = (options_parser & ~LYD_OPT_TYPEMASK);
509 autodetection = 1;
510 } else if (!strcmp(optarg, "config")) {
511 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
512 } else if (!strcmp(optarg, "get")) {
513 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
514 } else if (!strcmp(optarg, "getconfig")) {
515 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
516 } else if (!strcmp(optarg, "edit")) {
517 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
518 } else if (!strcmp(optarg, "data")) {
519 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
520 } else if (!strcmp(optarg, "rpc")) {
521 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
522 } else if (!strcmp(optarg, "rpcreply")) {
523 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
524 } else if (!strcmp(optarg, "notif")) {
525 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
526 } else {
527 fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg);
528 help(1);
529 goto cleanup;
530 }
531 break;
532#endif
533 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) {
611 if (!outformat_s) {
612 fprintf(stderr, "yanglint warning: --compiled option takes effect only in case of printing schemas.\n");
613 } else {
614 outformat_s++;
615 }
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 }
659#endif
660
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 */
694 if (!get_fileformat(argv[optind + i], &informat_s/* TODO, &informat_d */)) {
695 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);
712#if 0
713 } else {
714 if (autodetection && informat_d != LYD_XML) {
715 /* data file content autodetection is possible only for XML input */
716 fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n");
717 goto cleanup;
718 }
719
720 /* remember data filename and its format */
721 if (!data) {
722 data = data_item = malloc(sizeof *data);
723 } else {
724 for (data_item = data; data_item->next; data_item = data_item->next);
725 data_item->next = malloc(sizeof *data_item);
726 data_item = data_item->next;
727 }
728 data_item->filename = argv[optind + i];
729 data_item->format = informat_d;
730 data_item->type = options_parser & LYD_OPT_TYPEMASK;
731 data_item->tree = NULL;
732 data_item->xml = NULL;
733 data_item->next = NULL;
734#endif
735 }
736 }
737#if 0
738 if (outformat_d && !data && !list) {
739 fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n");
740 goto cleanup;
741 }
742#endif
743
744 /* enable specified features, if not specified, all the module's features are enabled */
745 u = 4; /* skip internal libyang modules */
746 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
747 for (i = 0; i < featsize; i++) {
748 if (!strcmp(feat[i], mod->name)) {
749 /* parse features spec */
750 featlist = strdup(feat[i] + strlen(feat[i]) + 1);
751 ptr = NULL;
752 while((ptr = strtok(ptr ? NULL : featlist, ","))) {
753 if (verbose >= 2) {
754 fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name);
755 }
756 if (lys_feature_enable(mod, ptr)) {
757 fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name);
758 }
759 }
760 free(featlist);
761 break;
762 }
763 }
764 if (i == featsize) {
765 if (verbose >= 2) {
766 fprintf(stdout, "Enabling all features in module %s.\n", mod->name);
767 }
768 lys_feature_enable(mod, "*");
769 }
770 }
771
772 /* convert (print) to FORMAT */
773 if (outformat_s) {
774 if (outformat_s == LYS_OUT_JSON && mods->count > 1) {
775 fputs("[", out);
776 }
777 for (u = 0; u < mods->count; u++) {
778 if (u) {
779 if (outformat_s == LYS_OUT_JSON) {
780 fputs(",\n", out);
781 } else {
782 fputs("\n", out);
783 }
784 }
785 lys_print_file(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s);
786 }
787 if (outformat_s == LYS_OUT_JSON) {
788 if (mods->count > 1) {
789 fputs("]\n", out);
790 } else if (mods->count == 1) {
791 fputs("\n", out);
792 }
793 }
794#if 0
795 } else if (data) {
796 ly_errno = 0;
797
798 /* prepare operational datastore when specified for RPC/Notification */
799 if (oper_file) {
800 /* get the file format */
801 if (!get_fileformat(oper_file, NULL, &informat_d)) {
802 goto cleanup;
803 } else if (!informat_d) {
804 fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n");
805 goto cleanup;
806 }
807 oper = lyd_parse_path(ctx, oper_file, informat_d, LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_TRUSTED);
808 if (!oper) {
809 fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n");
810 goto cleanup;
811 }
812 }
813
814 for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) {
815 /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */
816 if (autodetection) {
817 /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */
818 options_parser &= ~LYD_OPT_DATA_NO_YANGLIB;
819 /* automatically detect data type from the data top level */
820 data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0);
821 if (!data_item->xml) {
822 fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n");
823 goto cleanup;
824 }
825
826 /* NOTE: namespace is ignored to simplify usage of this feature */
827 if (!strcmp(data_item->xml->name, "data")) {
828 if (verbose >= 2) {
829 fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename);
830 }
831 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB;
832 data_item->type = LYD_OPT_DATA;
833 } else if (!strcmp(data_item->xml->name, "config")) {
834 if (verbose >= 2) {
835 fprintf(stdout, "Parsing %s as config data.\n", data_item->filename);
836 }
837 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
838 data_item->type = LYD_OPT_CONFIG;
839 } else if (!strcmp(data_item->xml->name, "get-reply")) {
840 if (verbose >= 2) {
841 fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename);
842 }
843 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
844 data_item->type = LYD_OPT_GET;
845 } else if (!strcmp(data_item->xml->name, "get-config-reply")) {
846 if (verbose >= 2) {
847 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename);
848 }
849 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
850 data_item->type = LYD_OPT_GETCONFIG;
851 } else if (!strcmp(data_item->xml->name, "edit-config")) {
852 if (verbose >= 2) {
853 fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename);
854 }
855 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
856 data_item->type = LYD_OPT_EDIT;
857 } else if (!strcmp(data_item->xml->name, "rpc")) {
858 if (verbose >= 2) {
859 fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename);
860 }
861 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
862 data_item->type = LYD_OPT_RPC;
863 } else if (!strcmp(data_item->xml->name, "rpc-reply")) {
864 if (verbose >= 2) {
865 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename);
866 }
867
868 data_item->type = LYD_OPT_RPCREPLY;
869 if (!data_item->next || (data_prev && !data_prev->tree)) {
870 fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
871 goto cleanup;
872 }
873
874 continue;
875 } else if (!strcmp(data_item->xml->name, "notification")) {
876 if (verbose >= 2) {
877 fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename);
878 }
879 options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
880 data_item->type = LYD_OPT_NOTIF;
881
882 /* ignore eventTime element if present */
883 while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) {
884 lyxml_free(ctx, data_item->xml->child);
885 }
886 } else {
887 fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n",
888 data_item->xml->name);
889 goto cleanup;
890 }
891
892 data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper);
893 if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) {
894parse_reply:
895 /* check result of the RPC parsing, we are going to do another parsing in this step */
896 if (ly_errno) {
897 goto cleanup;
898 }
899
900 /* check that we really have RPC for the reply */
901 if (data_item->type != LYD_OPT_RPC) {
902 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename);
903 goto cleanup;
904 }
905
906 if (data_prev->format == LYD_XML) {
907 /* ignore <ok> and <rpc-error> elements if present */
908 u = 0;
909 LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) {
910 if (!strcmp(data_prev->xml->child->name, "ok")) {
911 if (u) {
912 /* rpc-error or ok already present */
913 u = 0x8; /* error flag */
914 } else {
915 u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */
916 }
917 } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) {
918 if (u && (u & 0x1)) {
919 /* ok already present, rpc-error can be present multiple times */
920 u = 0x8; /* error flag */
921 } else {
922 u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */
923 }
924 }
925
926 if (u == 0x8) {
927 fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename);
928 goto cleanup;
929 } else if (u & 0x4) {
930 lyxml_free(ctx, data_prev->xml->child);
931 u &= ~0x4; /* unset lyxml_free() flag */
932 }
933 }
934
935 /* finally, parse RPC reply from the previous step */
936 data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child,
937 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
938 } else { /* LYD_JSON */
939 data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format,
940 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper);
941 }
942 }
943 } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) {
944 if (data_prev && !data_prev->tree) {
945 /* now we should have RPC for the preceding RPC reply */
946 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format,
947 (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper);
948 data_item->type = LYD_OPT_RPC;
949 goto parse_reply;
950 } else {
951 /* now we have RPC reply which will be parsed in next step together with its RPC */
952 if (!data_item->next) {
953 fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename);
954 goto cleanup;
955 }
956 if (data_item->format == LYD_XML) {
957 /* create rpc-reply container to unify handling with autodetection */
958 data_item->xml = calloc(1, sizeof *data_item->xml);
959 if (!data_item->xml) {
960 fprintf(stderr, "yanglint error: Memory allocation failed failed.\n");
961 goto cleanup;
962 }
963 data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9);
964 data_item->xml->prev = data_item->xml;
965 data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT);
966 if (data_item->xml->child) {
967 data_item->xml->child->parent = data_item->xml;
968 }
969 }
970 continue;
971 }
972 } else {
973 data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser, oper);
974 }
975 if (ly_errno) {
976 goto cleanup;
977 }
978
979 if (merge && data != data_item) {
980 if (!data->tree) {
981 data->tree = data_item->tree;
982 } else if (data_item->tree) {
983 /* merge results */
984 if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
985 fprintf(stderr, "yanglint error: merging multiple data trees failed.\n");
986 goto cleanup;
987 }
988 }
989 data_item->tree = NULL;
990 }
991 }
992
993 if (merge) {
994 /* validate the merged data tree, do not trust the input, invalidate all the data first */
995 LY_TREE_FOR(data->tree, subroot) {
996 LY_TREE_DFS_BEGIN(subroot, next, node) {
997 node->validity = LYD_VAL_OK;
998 switch (node->schema->nodetype) {
999 case LYS_LEAFLIST:
1000 case LYS_LEAF:
1001 if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) {
1002 node->validity |= LYD_VAL_LEAFREF;
1003 }
1004 break;
1005 case LYS_LIST:
1006 node->validity |= LYD_VAL_UNIQUE;
1007 /* falls through */
1008 case LYS_CONTAINER:
1009 case LYS_NOTIF:
1010 case LYS_RPC:
1011 case LYS_ACTION:
1012 node->validity |= LYD_VAL_MAND;
1013 break;
1014 default:
1015 break;
1016 }
1017 LY_TREE_DFS_END(subroot, next, node)
1018 }
1019 }
1020 if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) {
1021 goto cleanup;
1022 }
1023 }
1024
1025 /* print only if data output format specified */
1026 if (outformat_d) {
1027 for (data_item = data; data_item; data_item = data_item->next) {
1028 if (!merge && verbose >= 2) {
1029 fprintf(stdout, "File %s:\n", data_item->filename);
1030 }
1031 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 }
1064 lyd_print_file(out, (data_item->type == LYD_OPT_RPCREPLY) ? data_item->tree->child : data_item->tree,
1065 outformat_d, LYP_WITHSIBLINGS | LYP_FORMAT | options_dflt);
1066 if (envelope_s) {
1067 if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) {
1068 fprintf(out, "</action>\n");
1069 }
1070 fprintf(out, "</%s>\n", envelope_s);
1071 }
1072 if (merge) {
1073 /* stop after first item */
1074 break;
1075 }
1076 }
1077 }
1078#endif
1079 }
1080#if 0
1081 if (list) {
1082 print_list(out, ctx, outformat_d);
1083 }
1084#endif
1085
1086 ret = EXIT_SUCCESS;
1087
1088cleanup:
1089 if (out && out != stdout) {
1090 fclose(out);
1091 }
1092 ly_set_free(mods, NULL);
1093 ly_set_free(searchpaths, NULL);
1094 for (i = 0; i < featsize; i++) {
1095 free(feat[i]);
1096 }
1097 free(feat);
1098#if 0
1099 for (; data; data = data_item) {
1100 data_item = data->next;
1101 lyxml_free(ctx, data->xml);
1102 lyd_free_withsiblings(data->tree);
1103 free(data);
1104 }
1105 lyd_free_withsiblings(oper);
1106#endif
1107 ly_ctx_destroy(ctx, NULL);
1108
1109 return ret;
1110}