blob: 5d27c39af6bfd1bd1c0f16438a96e96b0a33b1ce [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file commands.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang's yanglint tool commands
5 *
6 * Copyright (c) 2015 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 <string.h>
18#include <stdio.h>
19#include <errno.h>
20#include <ctype.h>
21#include <assert.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25#include <getopt.h>
26#include <libgen.h>
27
28#include "commands.h"
29#include "libyang.h"
30
31COMMAND commands[];
32extern int done;
33extern struct ly_ctx *ctx;
34
35void
36cmd_add_help(void)
37{
38 printf("add [-i] <path-to-model> [<paths-to-other-models> ...]\n");
39 printf("\t-i - make all the imported modules implemented\n");
40}
41
42void
43cmd_load_help(void)
44{
45 printf("load [-i] <model-name> [<other-model-names> ...]\n");
46 printf("\t-i - make all the imported modules implemented\n");
47}
48
49void
50cmd_clear_help(void)
51{
52 printf("clear [<yang-library> | -e]\n");
53 printf("\t Replace the current context with an empty one, searchpaths are not kept.\n");
54 printf("\t If <yang-library> path specified, load the modules according to the yang library data.\n");
55 printf("\t Option '-e' causes ietf-yang-library will not be loaded.\n");
56}
57
58void
59cmd_print_help(void)
60{
61 printf("print [-f (yang | yin | tree [<tree-options>] | info [-P <info-path>] | jsons)] [-o <output-file>]"
62 " <model-name>[@<revision>]\n");
63 printf("\n");
64 printf("\ttree-options:\t--tree-print-groupings\t(print top-level groupings in a separate section)\n");
65 printf("\t \t--tree-print-uses\t(print uses nodes instead the resolved grouping nodes)\n");
66 printf("\t \t--tree-no-leafref-target\t(do not print the target nodes of leafrefs)\n");
67 printf("\t \t--tree-path <schema-path>\t(print only the specified subtree)\n");
68 printf("\t \t--tree-line-length <line-length>\t(wrap lines if longer than line-length,\n");
69 printf("\t \t\tnot a strict limit, longer lines can often appear)\n");
70 printf("\n");
71 printf("\tinfo-path:\t<schema-path> | typedef[<schema-path>]/<typedef-name> |\n");
72 printf("\t \t| identity/<identity-name> | feature/<feature-name> |\n");
73 printf("\t \t| grouping[<schema-path>]/<grouping-name> |\n");
74 printf("\t \t| type/<schema-path-leaf-or-leaflist>\n");
75 printf("\n");
76 printf("\tschema-path:\t( /<module-name>:<node-identifier> )+\n");
77}
78
79void
80cmd_data_help(void)
81{
82 printf("data [-(-s)trict] [-t TYPE] [-d DEFAULTS] [-o <output-file>] [-f (xml | json | lyb)] [-r <running-file-name>]\n");
83 printf(" <data-file-name> [<RPC/action-data-file-name> | <yang-data name>]\n\n");
84 printf("Accepted TYPEs:\n");
85 printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n");
86 printf("\t this option is applicable only in case of XML input data.\n");
87 printf("\tdata - LYD_OPT_DATA (default value) - complete datastore including status data.\n");
88 printf("\tconfig - LYD_OPT_CONFIG - complete configuration datastore.\n");
89 printf("\tget - LYD_OPT_GET - <get> operation result.\n");
90 printf("\tgetconfig - LYD_OPT_GETCONFIG - <get-config> operation result.\n");
91 printf("\tedit - LYD_OPT_EDIT - <edit-config>'s data (content of its <config> element).\n");
92 printf("\trpc - LYD_OPT_RPC - NETCONF RPC message.\n");
93 printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n");
94 printf("\tnotif - LYD_OPT_NOTIF - NETCONF Notification message.\n");
95 printf("\tyangdata - LYD_OPT_DATA_TEMPLATE - yang-data extension (last parameter mandatory in this case)\n\n");
96 printf("Accepted DEFAULTS:\n");
97 printf("\tall - add missing default nodes\n");
98 printf("\tall-tagged - add missing default nodes and mark all the default nodes with the attribute.\n");
99 printf("\ttrim - remove all nodes with a default value\n");
100 printf("\timplicit-tagged - add missing nodes and mark them with the attribute\n\n");
101 printf("Option -r:\n");
102 printf("\tOptional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the file contains running\n");
103 printf("\tconfiguration datastore data referenced from the RPC/Notification. Note that the file is\n");
104 printf("\tvalidated as 'data' TYPE. Special value '!' can be used as argument to ignore the\n");
105 printf("\texternal references.\n\n");
106 printf("\tIf an XPath expression (when/must) needs access to configuration data, you can provide\n");
107 printf("\tthem in a file, which will be parsed as 'data' TYPE.\n\n");
108}
109
110void
111cmd_xpath_help(void)
112{
113 printf("xpath [-t TYPE] [-x <additional-tree-file-name>] -e <XPath-expression>\n"
114 " <XML-data-file-name> [<JSON-rpc/action-schema-nodeid>]\n");
115 printf("Accepted TYPEs:\n");
116 printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n");
117 printf("\t this option is applicable only in case of XML input data.\n");
118 printf("\tconfig - LYD_OPT_CONFIG\n");
119 printf("\tget - LYD_OPT_GET\n");
120 printf("\tgetconfig - LYD_OPT_GETCONFIG\n");
121 printf("\tedit - LYD_OPT_EDIT\n");
122 printf("\trpc - LYD_OPT_RPC\n");
123 printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n");
124 printf("\tnotif - LYD_OPT_NOTIF\n\n");
125 printf("Option -x:\n");
126 printf("\tIf RPC/action/notification/RPC reply (for TYPEs 'rpc', 'rpcreply', and 'notif') includes\n");
127 printf("\tan XPath expression (when/must) that needs access to the configuration data, you can provide\n");
128 printf("\tthem in a file, which will be parsed as 'config'.\n");
129}
130
131void
132cmd_list_help(void)
133{
134 printf("list [-f (xml | json)]\n\n");
135 printf("\tBasic list output (no -f): i - imported module, I - implemented module\n");
136}
137
138void
139cmd_feature_help(void)
140{
141 printf("feature [ -(-e)nable | -(-d)isable (* | <feature-name>[,<feature-name> ...]) ] <model-name>[@<revision>]\n");
142}
143
144void
145cmd_searchpath_help(void)
146{
147 printf("searchpath [<model-dir-path> | --clear]\n\n");
148 printf("\tThey are used to search for imports and includes of a model.\n");
149 printf("\tThe \"load\" command uses these directories to find models directly.\n");
150}
151
152void
153cmd_verb_help(void)
154{
155 printf("verb (error/0 | warning/1 | verbose/2 | debug/3)\n");
156}
157
158#ifndef NDEBUG
159
160void
161cmd_debug_help(void)
162{
163 printf("debug (dict | yang | yin | xpath | diff)+\n");
164}
165
166#endif
167
168LYS_INFORMAT
169get_schema_format(const char *path)
170{
171 char *ptr;
172
173 if ((ptr = strrchr(path, '.')) != NULL) {
174 ++ptr;
175 if (!strcmp(ptr, "yang")) {
176 return LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200177 } else if (!strcmp(ptr, "yin")) {
178 return LYS_IN_YIN;
Radek Krejcied5acc52019-04-25 15:57:04 +0200179 } else {
180 fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr);
181 return LYS_IN_UNKNOWN;
182 }
183 } else {
184 fprintf(stdout, "Input file \"%s\" without extension - unknown format.\n", path);
185 return LYS_IN_UNKNOWN;
186 }
187}
188
189int
190cmd_add(const char *arg)
191{
192 int path_len, ret = 1, index = 0;
193 char *path, *dir, *s, *arg_ptr;
194 const char * const *searchpaths;
195 const struct lys_module *model;
196 LYS_INFORMAT format = LYS_IN_UNKNOWN;
197
198 if (strlen(arg) < 5) {
199 cmd_add_help();
200 return 1;
201 }
202
203 arg_ptr = strdup(arg + 3 /* ignore "add" */);
204
205 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
206 if (s[2] == '\0' || s[2] == ' ') {
Radek Krejci3fa46b62019-09-11 10:47:30 +0200207 ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200208 s[0] = s[1] = ' ';
209 }
210 }
211 s = arg_ptr;
212
213 while (arg_ptr[0] == ' ') {
214 ++arg_ptr;
215 }
216 if (strchr(arg_ptr, ' ')) {
217 path_len = strchr(arg_ptr, ' ') - arg_ptr;
218 } else {
219 path_len = strlen(arg_ptr);
220 }
221 path = strndup(arg_ptr, path_len);
222
223 searchpaths = ly_ctx_get_searchdirs(ctx);
224 if (searchpaths) {
225 for (index = 0; searchpaths[index]; index++);
226 }
227
228 while (path) {
229 format = get_schema_format(path);
230 if (format == LYS_IN_UNKNOWN) {
231 free(path);
232 goto cleanup;
233 }
234
235 dir = strdup(path);
236 ly_ctx_set_searchdir(ctx, dirname(dir));
237 model = lys_parse_path(ctx, path, format);
238 ly_ctx_unset_searchdir(ctx, index);
239 free(path);
240 free(dir);
241
242 if (!model) {
243 /* libyang printed the error messages */
244 goto cleanup;
245 }
246
247 /* next model */
248 arg_ptr += path_len;
249 while (arg_ptr[0] == ' ') {
250 ++arg_ptr;
251 }
252 if (strchr(arg_ptr, ' ')) {
253 path_len = strchr(arg_ptr, ' ') - arg_ptr;
254 } else {
255 path_len = strlen(arg_ptr);
256 }
257
258 if (path_len) {
259 path = strndup(arg_ptr, path_len);
260 } else {
261 path = NULL;
262 }
263 }
264 if (format == LYS_IN_UNKNOWN) {
265 /* no schema on input */
266 cmd_add_help();
267 goto cleanup;
268 }
269 ret = 0;
270
271cleanup:
272 free(s);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200273 ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200274
275 return ret;
276}
277
278int
279cmd_load(const char *arg)
280{
281 int name_len, ret = 1;
282 char *name, *s, *arg_ptr;
283 const struct lys_module *model;
284
285 if (strlen(arg) < 6) {
286 cmd_load_help();
287 return 1;
288 }
289
290 arg_ptr = strdup(arg + 4 /* ignore "load" */);
291
292 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
293 if (s[2] == '\0' || s[2] == ' ') {
Radek Krejci3fa46b62019-09-11 10:47:30 +0200294 ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200295 s[0] = s[1] = ' ';
296 }
297 }
298 s = arg_ptr;
299
300 while (arg_ptr[0] == ' ') {
301 ++arg_ptr;
302 }
303 if (strchr(arg_ptr, ' ')) {
304 name_len = strchr(arg_ptr, ' ') - arg_ptr;
305 } else {
306 name_len = strlen(arg_ptr);
307 }
308 name = strndup(arg_ptr, name_len);
309
310 while (name) {
311 model = ly_ctx_load_module(ctx, name, NULL);
312 free(name);
313 if (!model) {
314 /* libyang printed the error messages */
315 goto cleanup;
316 }
317
318 /* next model */
319 arg_ptr += name_len;
320 while (arg_ptr[0] == ' ') {
321 ++arg_ptr;
322 }
323 if (strchr(arg_ptr, ' ')) {
324 name_len = strchr(arg_ptr, ' ') - arg_ptr;
325 } else {
326 name_len = strlen(arg_ptr);
327 }
328
329 if (name_len) {
330 name = strndup(arg_ptr, name_len);
331 } else {
332 name = NULL;
333 }
334 }
335 ret = 0;
336
337cleanup:
338 free(s);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200339 ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200340
341 return ret;
342}
343
344int
345cmd_print(const char *arg)
346{
Radek Krejci693262f2019-04-29 15:23:20 +0200347 int c, argc, option_index, ret = 1, tree_ll = 0, tree_opts = 0, compiled = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200348 char **argv = NULL, *ptr, *model_name, *revision;
349 const char *out_path = NULL;
350 const struct lys_module *module;
351 LYS_OUTFORMAT format = LYS_OUT_TREE;
352 FILE *output = stdout;
353 static struct option long_options[] = {
Radek Krejci693262f2019-04-29 15:23:20 +0200354 {"compiled", no_argument, 0, 'c'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200355 {"help", no_argument, 0, 'h'},
356 {"format", required_argument, 0, 'f'},
357 {"output", required_argument, 0, 'o'},
358#if 0
359 {"tree-print-groupings", no_argument, 0, 'g'},
360 {"tree-print-uses", no_argument, 0, 'u'},
361 {"tree-no-leafref-target", no_argument, 0, 'n'},
362 {"tree-path", required_argument, 0, 'P'},
363 {"info-path", required_argument, 0, 'P'},
364 {"tree-line-length", required_argument, 0, 'L'},
365#endif
366 {NULL, 0, 0, 0}
367 };
368 void *rlcd;
369
370 argc = 1;
371 argv = malloc(2*sizeof *argv);
372 *argv = strdup(arg);
373 ptr = strtok(*argv, " ");
374 while ((ptr = strtok(NULL, " "))) {
375 rlcd = realloc(argv, (argc+2)*sizeof *argv);
376 if (!rlcd) {
377 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
378 goto cleanup;
379 }
380 argv = rlcd;
381 argv[argc++] = ptr;
382 }
383 argv[argc] = NULL;
384
385 optind = 0;
386 while (1) {
387 option_index = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200388 c = getopt_long(argc, argv, "chf:go:guP:L:", long_options, &option_index);
Radek Krejcied5acc52019-04-25 15:57:04 +0200389 if (c == -1) {
390 break;
391 }
392
393 switch (c) {
Radek Krejci693262f2019-04-29 15:23:20 +0200394 case 'c':
395 compiled = 1;
396 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200397 case 'h':
398 cmd_print_help();
399 ret = 0;
400 goto cleanup;
401 case 'f':
402 if (!strcmp(optarg, "yang")) {
403 format = LYS_OUT_YANG;
404#if 0
405 } else if (!strcmp(optarg, "yin")) {
406 format = LYS_OUT_YIN;
407 } else if (!strcmp(optarg, "tree")) {
408 format = LYS_OUT_TREE;
409 } else if (!strcmp(optarg, "tree-rfc")) {
410 format = LYS_OUT_TREE;
411 tree_opts |= LYS_OUTOPT_TREE_RFC;
412 } else if (!strcmp(optarg, "info")) {
413 format = LYS_OUT_INFO;
414 } else if (!strcmp(optarg, "jsons")) {
415 format = LYS_OUT_JSON;
416#endif
417 } else {
418 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
419 goto cleanup;
420 }
421 break;
422 case 'o':
423 if (out_path) {
424 fprintf(stderr, "Output specified twice.\n");
425 goto cleanup;
426 }
427 out_path = optarg;
428 break;
429#if 0
430 case 'g':
431 tree_opts |= LYS_OUTOPT_TREE_GROUPING;
432 break;
433 case 'u':
434 tree_opts |= LYS_OUTOPT_TREE_USES;
435 break;
436 case 'n':
437 tree_opts |= LYS_OUTOPT_TREE_NO_LEAFREF;
438 break;
439 case 'P':
440 target_path = optarg;
441 break;
442 case 'L':
443 tree_ll = atoi(optarg);
444 break;
445#endif
446 case '?':
447 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
448 goto cleanup;
449 }
450 }
451
452 /* file name */
453 if (optind == argc) {
454 fprintf(stderr, "Missing the module name.\n");
455 goto cleanup;
456 }
457
Radek Krejci693262f2019-04-29 15:23:20 +0200458 /* compiled format */
459 if (compiled) {
460 format++;
461 }
Radek Krejci77954e82019-04-30 13:51:29 +0200462#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200463 /* tree fromat with or without gropings */
464 if ((tree_opts || tree_ll) && format != LYS_OUT_TREE) {
465 fprintf(stderr, "--tree options take effect only in case of the tree output format.\n");
466 }
Radek Krejci77954e82019-04-30 13:51:29 +0200467#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200468 /* module, revision */
469 model_name = argv[optind];
470 revision = NULL;
471 if (strchr(model_name, '@')) {
472 revision = strchr(model_name, '@');
473 revision[0] = '\0';
474 ++revision;
475 }
476
477 if (revision) {
478 module = ly_ctx_get_module(ctx, model_name, revision);
479 } else {
480 module = ly_ctx_get_module_latest(ctx, model_name);
481 }
482#if 0
483 if (!module) {
484 /* not a module, try to find it as a submodule */
485 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
486 }
487#endif
488
489 if (!module) {
490 if (revision) {
491 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
492 } else {
493 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
494 }
495 goto cleanup;
496 }
497
498 if (out_path) {
499 output = fopen(out_path, "w");
500 if (!output) {
501 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
502 goto cleanup;
503 }
504 }
505
506 ret = lys_print_file(output, module, format, tree_ll, tree_opts);
507 if (format == LYS_OUT_JSON) {
508 fputs("\n", output);
509 }
510
511cleanup:
512 free(*argv);
513 free(argv);
514
515 if (output && (output != stdout)) {
516 fclose(output);
517 }
518
519 return ret;
520}
Radek Krejcie7b95092019-05-15 11:03:07 +0200521
Radek Krejcied5acc52019-04-25 15:57:04 +0200522static LYD_FORMAT
523detect_data_format(char *filepath)
524{
525 size_t len;
526
527 /* detect input format according to file suffix */
528 len = strlen(filepath);
529 for (; isspace(filepath[len - 1]); len--, filepath[len] = '\0'); /* remove trailing whitespaces */
530 if (len >= 5 && !strcmp(&filepath[len - 4], ".xml")) {
531 return LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200532#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200533 } else if (len >= 6 && !strcmp(&filepath[len - 5], ".json")) {
534 return LYD_JSON;
535 } else if (len >= 5 && !strcmp(&filepath[len - 4], ".lyb")) {
536 return LYD_LYB;
Radek Krejcie7b95092019-05-15 11:03:07 +0200537#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200538 } else {
539 return LYD_UNKNOWN;
540 }
541}
542
543static int
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200544parse_data(char *filepath, int *options, const struct lyd_node **trees, const char *rpc_act_file,
Radek Krejcied5acc52019-04-25 15:57:04 +0200545 struct lyd_node **result)
546{
547 LYD_FORMAT informat = LYD_UNKNOWN;
Radek Krejcied5acc52019-04-25 15:57:04 +0200548 struct lyd_node *data = NULL, *rpc_act = NULL;
549 int opts = *options;
550
551 /* detect input format according to file suffix */
552 informat = detect_data_format(filepath);
553 if (informat == LYD_UNKNOWN) {
554 fprintf(stderr, "Unable to resolve format of the input file, please add \".xml\", \".json\", or \".lyb\" suffix.\n");
555 return EXIT_FAILURE;
556 }
557
Radek Krejcie7b95092019-05-15 11:03:07 +0200558 ly_err_clean(ctx, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200559
Radek Krejcie7b95092019-05-15 11:03:07 +0200560#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200561 if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) {
562 /* automatically detect data type from the data top level */
563 if (informat != LYD_XML) {
564 fprintf(stderr, "Only XML data can be automatically explored.\n");
565 return EXIT_FAILURE;
566 }
567
568 xml = lyxml_parse_path(ctx, filepath, 0);
569 if (!xml) {
570 fprintf(stderr, "Failed to parse XML data for automatic type detection.\n");
571 return EXIT_FAILURE;
572 }
573
574 /* NOTE: namespace is ignored to simplify usage of this feature */
575
576 if (!strcmp(xml->name, "data")) {
577 fprintf(stdout, "Parsing %s as complete datastore.\n", filepath);
578 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_ADD_YANGLIB;
579 } else if (!strcmp(xml->name, "config")) {
580 fprintf(stdout, "Parsing %s as config data.\n", filepath);
581 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
582 } else if (!strcmp(xml->name, "get-reply")) {
583 fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath);
584 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
585 } else if (!strcmp(xml->name, "get-config-reply")) {
586 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath);
587 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
588 } else if (!strcmp(xml->name, "edit-config")) {
589 fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath);
590 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
591 } else if (!strcmp(xml->name, "rpc")) {
592 fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath);
593 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
594 } else if (!strcmp(xml->name, "rpc-reply")) {
595 if (!rpc_act_file) {
596 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
597 lyxml_free(ctx, xml);
598 return EXIT_FAILURE;
599 }
600 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath);
601 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
602 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
603 if (!rpc_act) {
604 fprintf(stderr, "Failed to parse RPC/action.\n");
605 lyxml_free(ctx, xml);
606 return EXIT_FAILURE;
607 }
608 } else if (!strcmp(xml->name, "notification")) {
609 fprintf(stdout, "Parsing %s as <notification> data.\n", filepath);
610 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
611 } else if (!strcmp(xml->name, "yang-data")) {
612 fprintf(stdout, "Parsing %s as <yang-data> data.\n", filepath);
613 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
614 if (!rpc_act_file) {
615 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
616 lyxml_free(ctx, xml);
617 return EXIT_FAILURE;
618 }
619 } else {
620 fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n");
621 lyxml_free(ctx, xml);
622 return EXIT_FAILURE;
623 }
624
625 if (opts & LYD_OPT_RPCREPLY) {
626 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act, val_tree);
627 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
628 data = lyd_parse_xml(ctx, &xml->child, opts, val_tree);
629 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
630 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act_file);
631 } else {
632 data = lyd_parse_xml(ctx, &xml->child, opts);
633 }
634 lyxml_free(ctx, xml);
635 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200636#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200637 if (opts & LYD_OPT_RPCREPLY) {
638 if (!rpc_act_file) {
639 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
640 return EXIT_FAILURE;
641 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200642 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200643 if (!rpc_act) {
644 fprintf(stderr, "Failed to parse RPC/action.\n");
645 return EXIT_FAILURE;
646 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200647 if (trees) {
648 const struct lyd_node **trees_new;
649 unsigned int u;
650 trees_new = lyd_trees_new(1, rpc_act);
651
652 LY_ARRAY_FOR(trees, u) {
653 trees_new = lyd_trees_add(trees_new, trees[u]);
654 }
655 lyd_trees_free(trees, 0);
656 trees = trees_new;
657 } else {
658 trees = lyd_trees_new(1, rpc_act);
659 }
660 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200661 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200662 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
663#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200664 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
665 if (!rpc_act_file) {
666 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
667 return EXIT_FAILURE;
668 }
669 data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file);
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200670#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200671 } else {
672 if (!(opts & LYD_OPT_TYPEMASK)) {
673 /* automatically add yang-library data */
674 opts |= LYD_OPT_DATA_ADD_YANGLIB;
675 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200676 data = lyd_parse_path(ctx, filepath, informat, opts, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200677 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200678#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200679 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200680#endif
681 lyd_free_all(rpc_act);
Radek Krejcied5acc52019-04-25 15:57:04 +0200682
Radek Krejcie7b95092019-05-15 11:03:07 +0200683 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200684 fprintf(stderr, "Failed to parse data.\n");
Radek Krejcie7b95092019-05-15 11:03:07 +0200685 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200686 return EXIT_FAILURE;
687 }
688
689 *result = data;
690 *options = opts;
691 return EXIT_SUCCESS;
692}
693
694int
695cmd_data(const char *arg)
696{
697 int c, argc, option_index, ret = 1;
698 int options = 0, printopt = 0;
699 char **argv = NULL, *ptr;
700 const char *out_path = NULL;
701 struct lyd_node *data = NULL, *val_tree = NULL;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200702 const struct lyd_node **trees = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200703 LYD_FORMAT outformat = LYD_UNKNOWN;
704 FILE *output = stdout;
705 static struct option long_options[] = {
706 {"defaults", required_argument, 0, 'd'},
707 {"help", no_argument, 0, 'h'},
708 {"format", required_argument, 0, 'f'},
709 {"option", required_argument, 0, 't'},
710 {"output", required_argument, 0, 'o'},
711 {"running", required_argument, 0, 'r'},
712 {"strict", no_argument, 0, 's'},
713 {NULL, 0, 0, 0}
714 };
715 void *rlcd;
716
717 argc = 1;
718 argv = malloc(2*sizeof *argv);
719 *argv = strdup(arg);
720 ptr = strtok(*argv, " ");
721 while ((ptr = strtok(NULL, " "))) {
722 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
723 if (!rlcd) {
724 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
725 goto cleanup;
726 }
727 argv = rlcd;
728 argv[argc++] = ptr;
729 }
730 argv[argc] = NULL;
731
732 optind = 0;
733 while (1) {
734 option_index = 0;
735 c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index);
736 if (c == -1) {
737 break;
738 }
739
740 switch (c) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200741#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200742 case 'd':
743 if (!strcmp(optarg, "all")) {
744 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
745 } else if (!strcmp(optarg, "all-tagged")) {
746 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
747 } else if (!strcmp(optarg, "trim")) {
748 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;
749 } else if (!strcmp(optarg, "implicit-tagged")) {
750 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
751 }
752 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200753#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200754 case 'h':
755 cmd_data_help();
756 ret = 0;
757 goto cleanup;
758 case 'f':
759 if (!strcmp(optarg, "xml")) {
760 outformat = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200761#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200762 } else if (!strcmp(optarg, "json")) {
763 outformat = LYD_JSON;
764 } else if (!strcmp(optarg, "lyb")) {
765 outformat = LYD_LYB;
Radek Krejcie7b95092019-05-15 11:03:07 +0200766#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200767 } else {
768 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
769 goto cleanup;
770 }
771 break;
772 case 'o':
773 if (out_path) {
774 fprintf(stderr, "Output specified twice.\n");
775 goto cleanup;
776 }
777 out_path = optarg;
778 break;
779 case 'r':
Radek Krejcied5acc52019-04-25 15:57:04 +0200780 if (optarg[0] == '!') {
781 /* ignore extenral dependencies to the running datastore */
782 options |= LYD_OPT_NOEXTDEPS;
783 } else {
784 /* external file with the running datastore */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200785 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200786 if (!val_tree) {
787 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
788 goto cleanup;
789 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200790 if (!trees) {
791 trees = lyd_trees_new(1, val_tree);
792 } else {
793 trees = lyd_trees_add(trees, val_tree);
794 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200795 }
796 break;
797 case 's':
798 options |= LYD_OPT_STRICT;
799 options |= LYD_OPT_OBSOLETE;
800 break;
801 case 't':
802 if (!strcmp(optarg, "auto")) {
803 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
804 } else if (!strcmp(optarg, "data")) {
805 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA;
806 } else if (!strcmp(optarg, "config")) {
807 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
808 } else if (!strcmp(optarg, "get")) {
809 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
810 } else if (!strcmp(optarg, "getconfig")) {
811 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
812 } else if (!strcmp(optarg, "edit")) {
813 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
814 } else if (!strcmp(optarg, "rpc")) {
815 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
816 } else if (!strcmp(optarg, "rpcreply")) {
817 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
818 } else if (!strcmp(optarg, "notif")) {
819 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
820 } else if (!strcmp(optarg, "yangdata")) {
821 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
822 } else {
823 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
824 cmd_data_help();
825 goto cleanup;
826 }
827 break;
828 case '?':
829 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
830 goto cleanup;
831 }
832 }
833
834 /* file name */
835 if (optind == argc) {
836 fprintf(stderr, "Missing the data file name.\n");
837 goto cleanup;
838 }
839
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200840 if (parse_data(argv[optind], &options, trees, argv[optind + 1], &data)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200841 goto cleanup;
842 }
843
844 if (out_path) {
845 output = fopen(out_path, "w");
846 if (!output) {
847 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
848 goto cleanup;
849 }
850 }
851
852 if (outformat != LYD_UNKNOWN) {
853 if (options & LYD_OPT_RPCREPLY) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200854 lyd_print_file(output, lyd_node_children(data), outformat, LYDP_WITHSIBLINGS | LYDP_FORMAT | printopt);
Radek Krejcied5acc52019-04-25 15:57:04 +0200855 } else {
Radek Krejcie7b95092019-05-15 11:03:07 +0200856 lyd_print_file(output, data, outformat, LYDP_WITHSIBLINGS | LYDP_FORMAT | printopt);
Radek Krejcied5acc52019-04-25 15:57:04 +0200857 }
858 }
859
860 ret = 0;
861
862cleanup:
863 free(*argv);
864 free(argv);
865
866 if (output && (output != stdout)) {
867 fclose(output);
868 }
869
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200870 lyd_trees_free(trees, 1);
Radek Krejcie7b95092019-05-15 11:03:07 +0200871 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200872
873 return ret;
874}
Radek Krejcie7b95092019-05-15 11:03:07 +0200875#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200876int
877cmd_xpath(const char *arg)
878{
879 int c, argc, option_index, ret = 1, long_str;
880 char **argv = NULL, *ptr, *expr = NULL;
881 unsigned int i, j;
882 int options = 0;
883 struct lyd_node *data = NULL, *node, *val_tree = NULL;
884 struct lyd_node_leaf_list *key;
885 struct ly_set *set;
886 static struct option long_options[] = {
887 {"help", no_argument, 0, 'h'},
888 {"expr", required_argument, 0, 'e'},
889 {NULL, 0, 0, 0}
890 };
891 void *rlcd;
892
893 long_str = 0;
894 argc = 1;
895 argv = malloc(2 * sizeof *argv);
896 *argv = strdup(arg);
897 ptr = strtok(*argv, " ");
898 while ((ptr = strtok(NULL, " "))) {
899 if (long_str) {
900 ptr[-1] = ' ';
901 if (ptr[strlen(ptr) - 1] == long_str) {
902 long_str = 0;
903 ptr[strlen(ptr) - 1] = '\0';
904 }
905 } else {
906 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
907 if (!rlcd) {
908 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
909 goto cleanup;
910 }
911 argv = rlcd;
912 argv[argc] = ptr;
913 if (ptr[0] == '"') {
914 long_str = '"';
915 ++argv[argc];
916 }
917 if (ptr[0] == '\'') {
918 long_str = '\'';
919 ++argv[argc];
920 }
921 if (ptr[strlen(ptr) - 1] == long_str) {
922 long_str = 0;
923 ptr[strlen(ptr) - 1] = '\0';
924 }
925 ++argc;
926 }
927 }
928 argv[argc] = NULL;
929
930 optind = 0;
931 while (1) {
932 option_index = 0;
933 c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index);
934 if (c == -1) {
935 break;
936 }
937
938 switch (c) {
939 case 'h':
940 cmd_xpath_help();
941 ret = 0;
942 goto cleanup;
943 case 'e':
944 expr = optarg;
945 break;
946 case 't':
947 if (!strcmp(optarg, "auto")) {
948 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
949 } else if (!strcmp(optarg, "config")) {
950 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
951 } else if (!strcmp(optarg, "get")) {
952 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
953 } else if (!strcmp(optarg, "getconfig")) {
954 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
955 } else if (!strcmp(optarg, "edit")) {
956 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
957 } else if (!strcmp(optarg, "rpc")) {
958 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
959 } else if (!strcmp(optarg, "rpcreply")) {
960 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
961 } else if (!strcmp(optarg, "notif")) {
962 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
963 } else if (!strcmp(optarg, "yangdata")) {
964 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
965 } else {
966 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
967 cmd_data_help();
968 goto cleanup;
969 }
970 break;
971 case 'x':
972 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG);
973 if (!val_tree) {
974 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
975 goto cleanup;
976 }
977 break;
978 case '?':
979 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
980 goto cleanup;
981 }
982 }
983
984 if (optind == argc) {
985 fprintf(stderr, "Missing the file with data.\n");
986 goto cleanup;
987 }
988
989 if (!expr) {
990 fprintf(stderr, "Missing the XPath expression.\n");
991 goto cleanup;
992 }
993
994 if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
995 goto cleanup;
996 }
997
998 if (!(set = lyd_find_path(data, expr))) {
999 goto cleanup;
1000 }
1001
1002 /* print result */
1003 printf("Result:\n");
1004 if (!set->number) {
1005 printf("\tEmpty\n");
1006 } else {
1007 for (i = 0; i < set->number; ++i) {
1008 node = set->set.d[i];
1009 switch (node->schema->nodetype) {
1010 case LYS_CONTAINER:
1011 printf("\tContainer ");
1012 break;
1013 case LYS_LEAF:
1014 printf("\tLeaf ");
1015 break;
1016 case LYS_LEAFLIST:
1017 printf("\tLeaflist ");
1018 break;
1019 case LYS_LIST:
1020 printf("\tList ");
1021 break;
1022 case LYS_ANYXML:
1023 printf("\tAnyxml ");
1024 break;
1025 case LYS_ANYDATA:
1026 printf("\tAnydata ");
1027 break;
1028 default:
1029 printf("\tUnknown ");
1030 break;
1031 }
1032 printf("\"%s\"", node->schema->name);
1033 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1034 printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str);
1035 } else if (node->schema->nodetype == LYS_LIST) {
1036 key = (struct lyd_node_leaf_list *)node->child;
1037 printf(" (");
1038 for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) {
1039 if (j) {
1040 printf(" ");
1041 }
1042 printf("\"%s\": %s", key->schema->name, key->value_str);
1043 key = (struct lyd_node_leaf_list *)key->next;
1044 }
1045 printf(")");
1046 }
1047 printf("\n");
1048 }
1049 }
1050 printf("\n");
1051
1052 ly_set_free(set);
1053 ret = 0;
1054
1055cleanup:
1056 free(*argv);
1057 free(argv);
1058
1059 lyd_free_withsiblings(data);
1060
1061 return ret;
1062}
1063
1064int
1065print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
1066{
1067 struct lyd_node *ylib;
1068 uint32_t idx = 0, has_modules = 0;
1069 uint8_t u;
1070 const struct lys_module *mod;
1071
1072 if (outformat != LYD_UNKNOWN) {
1073 ylib = ly_ctx_info(ctx);
1074 if (!ylib) {
1075 fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n");
1076 return 1;
1077 }
1078
1079 lyd_print_file(out, ylib, outformat, LYP_WITHSIBLINGS | LYP_FORMAT);
1080 lyd_free_withsiblings(ylib);
1081 return 0;
1082 }
1083
1084 /* iterate schemas in context and provide just the basic info */
1085 fprintf(out, "List of the loaded models:\n");
1086 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
1087 has_modules++;
1088
1089 /* conformance print */
1090 if (mod->implemented) {
1091 fprintf(out, "\tI");
1092 } else {
1093 fprintf(out, "\ti");
1094 }
1095
1096 /* module print */
1097 fprintf(out, " %s", mod->name);
1098 if (mod->rev_size) {
1099 fprintf(out, "@%s", mod->rev[0].date);
1100 }
1101
1102 /* submodules print */
1103 if (mod->inc_size) {
1104 fprintf(out, " (");
1105 for (u = 0; u < mod->inc_size; u++) {
1106 fprintf(out, "%s%s", !u ? "" : ",", mod->inc[u].submodule->name);
1107 if (mod->inc[u].submodule->rev_size) {
1108 fprintf(out, "@%s", mod->inc[u].submodule->rev[0].date);
1109 }
1110 }
1111 fprintf(out, ")");
1112 }
1113
1114 /* finish the line */
1115 fprintf(out, "\n");
1116 }
1117
1118 if (!has_modules) {
1119 fprintf(out, "\t(none)\n");
1120 }
1121
1122 return 0;
1123}
1124
1125int
1126cmd_list(const char *arg)
1127{
1128 char **argv = NULL, *ptr;
1129 int c, argc, option_index;
1130 LYD_FORMAT outformat = LYD_UNKNOWN;
1131 static struct option long_options[] = {
1132 {"help", no_argument, 0, 'h'},
1133 {"format", required_argument, 0, 'f'},
1134 {NULL, 0, 0, 0}
1135 };
1136 void *rlcd;
1137
1138 argc = 1;
1139 argv = malloc(2*sizeof *argv);
1140 *argv = strdup(arg);
1141 ptr = strtok(*argv, " ");
1142 while ((ptr = strtok(NULL, " "))) {
1143 rlcd = realloc(argv, (argc+2)*sizeof *argv);
1144 if (!rlcd) {
1145 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1146 goto error;
1147 }
1148 argv = rlcd;
1149 argv[argc++] = ptr;
1150 }
1151 argv[argc] = NULL;
1152
1153 optind = 0;
1154 while (1) {
1155 option_index = 0;
1156 c = getopt_long(argc, argv, "hf:", long_options, &option_index);
1157 if (c == -1) {
1158 break;
1159 }
1160
1161 switch (c) {
1162 case 'h':
1163 cmd_data_help();
1164 free(*argv);
1165 free(argv);
1166 return 0;
1167 case 'f':
1168 if (!strcmp(optarg, "xml")) {
1169 outformat = LYD_XML;
1170 } else if (!strcmp(optarg, "json")) {
1171 outformat = LYD_JSON;
1172 } else {
1173 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
1174 goto error;
1175 }
1176 break;
1177 case '?':
1178 /* getopt_long() prints message */
1179 goto error;
1180 }
1181 }
1182 if (optind != argc) {
1183 fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]);
1184error:
1185 free(*argv);
1186 free(argv);
1187 return 1;
1188 }
1189 free(*argv);
1190 free(argv);
1191
1192 return print_list(stdout, ctx, outformat);
1193}
1194#endif
1195int
1196cmd_feature(const char *arg)
1197{
1198 int c, argc, option_index, ret = 1, task = 0;
1199 char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL;
1200 const struct lys_module *module;
1201 static struct option long_options[] = {
1202 {"help", no_argument, 0, 'h'},
1203 {"enable", required_argument, 0, 'e'},
1204 {"disable", required_argument, 0, 'd'},
1205 {NULL, 0, 0, 0}
1206 };
1207 void *rlcd;
1208
1209 argc = 1;
1210 argv = malloc(2*sizeof *argv);
1211 *argv = strdup(arg);
1212 ptr = strtok(*argv, " ");
1213 while ((ptr = strtok(NULL, " "))) {
1214 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
1215 if (!rlcd) {
1216 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1217 goto cleanup;
1218 }
1219 argv = rlcd;
1220 argv[argc++] = ptr;
1221 }
1222 argv[argc] = NULL;
1223
1224 optind = 0;
1225 while (1) {
1226 option_index = 0;
1227 c = getopt_long(argc, argv, "he:d:", long_options, &option_index);
1228 if (c == -1) {
1229 break;
1230 }
1231
1232 switch (c) {
1233 case 'h':
1234 cmd_feature_help();
1235 ret = 0;
1236 goto cleanup;
1237 case 'e':
1238 if (task) {
1239 fprintf(stderr, "Only one of enable or disable can be specified.\n");
1240 goto cleanup;
1241 }
1242 task = 1;
1243 feat_names = optarg;
1244 break;
1245 case 'd':
1246 if (task) {
1247 fprintf(stderr, "Only one of enable, or disable can be specified.\n");
1248 goto cleanup;
1249 }
1250 task = 2;
1251 feat_names = optarg;
1252 break;
1253 case '?':
1254 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
1255 goto cleanup;
1256 }
1257 }
1258
1259 /* module name */
1260 if (optind == argc) {
1261 fprintf(stderr, "Missing the module name.\n");
1262 goto cleanup;
1263 }
1264
1265 revision = NULL;
1266 model_name = argv[optind];
1267 if (strchr(model_name, '@')) {
1268 revision = strchr(model_name, '@');
1269 revision[0] = '\0';
1270 ++revision;
1271 }
1272
1273 module = ly_ctx_get_module(ctx, model_name, revision);
1274#if 0
1275 if (!module) {
1276 /* not a module, try to find it as a submodule */
1277 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
1278 }
1279#endif
1280
1281 if (module == NULL) {
1282 if (revision) {
1283 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
1284 } else {
1285 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
1286 }
1287 goto cleanup;
1288 }
1289
1290 if (!task) {
1291 unsigned int len, max_len = 0;
1292 unsigned int u;
1293 struct lysc_feature *features;
1294
1295 printf("%s features:\n", module->name);
1296
1297 if (module->compiled) {
1298 features = module->compiled->features;
1299 } else {
1300 features = module->off_features;
1301 }
1302
1303 /* get the max len */
1304 LY_ARRAY_FOR(features, u) {
1305 len = strlen(features[u].name);
1306 if (len > max_len) {
1307 max_len = len;
1308 }
1309 }
1310
1311 LY_ARRAY_FOR(features, u) {
1312 printf("\t%-*s (%s)\n", max_len, features[u].name, (features[u].flags & LYS_FENABLED) ? "on" : "off");
1313 }
1314 if (!u) {
1315 printf("\t(none)\n");
1316 }
1317 } else {
1318 feat_names = strtok(feat_names, ",");
1319 while (feat_names) {
1320 if (((task == 1) && lys_feature_enable(module, feat_names))
1321 || ((task == 2) && lys_feature_disable(module, feat_names))) {
1322 fprintf(stderr, "Feature \"%s\" not found.\n", feat_names);
1323 ret = 1;
1324 }
1325 feat_names = strtok(NULL, ",");
1326 }
1327 }
1328
1329cleanup:
1330 free(*argv);
1331 free(argv);
1332
1333 return ret;
1334}
1335
1336int
1337cmd_searchpath(const char *arg)
1338{
1339 const char *path;
1340 const char * const *searchpaths;
1341 int index;
1342 struct stat st;
1343
1344 for (path = strchr(arg, ' '); path && (path[0] == ' '); ++path);
1345 if (!path || (path[0] == '\0')) {
1346 searchpaths = ly_ctx_get_searchdirs(ctx);
1347 if (searchpaths) {
1348 for (index = 0; searchpaths[index]; index++) {
1349 fprintf(stdout, "%s\n", searchpaths[index]);
1350 }
1351 }
1352 return 0;
1353 }
1354
1355 if ((!strncmp(path, "-h", 2) && (path[2] == '\0' || path[2] == ' ')) ||
1356 (!strncmp(path, "--help", 6) && (path[6] == '\0' || path[6] == ' '))) {
1357 cmd_searchpath_help();
1358 return 0;
1359 } else if (!strncmp(path, "--clear", 7) && (path[7] == '\0' || path[7] == ' ')) {
1360 ly_ctx_unset_searchdirs(ctx, NULL);
1361 return 0;
1362 }
1363
1364 if (stat(path, &st) == -1) {
1365 fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno));
1366 return 1;
1367 }
1368 if (!S_ISDIR(st.st_mode)) {
1369 fprintf(stderr, "\"%s\" is not a directory.\n", path);
1370 return 1;
1371 }
1372
1373 ly_ctx_set_searchdir(ctx, path);
1374
1375 return 0;
1376}
1377
1378int
1379cmd_clear(const char *arg)
1380{
1381 struct ly_ctx *ctx_new;
1382 int options = 0;
1383#if 0
1384 int i;
1385 char *ylpath;
1386 const char * const *searchpaths;
1387 LYD_FORMAT format;
1388
1389 /* get optional yang library file name */
1390 for (i = 5; arg[i] && isspace(arg[i]); i++);
1391 if (arg[i]) {
1392 if (arg[i] == '-' && arg[i + 1] == 'e') {
1393 options = LY_CTX_NOYANGLIBRARY;
1394 goto create_empty;
1395 } else {
1396 ylpath = strdup(&arg[i]);
1397 format = detect_data_format(ylpath);
1398 if (format == LYD_UNKNOWN) {
1399 free(ylpath);
1400 fprintf(stderr, "Unable to resolve format of the yang library file, please add \".xml\" or \".json\" suffix.\n");
1401 goto create_empty;
1402 }
1403 searchpaths = ly_ctx_get_searchdirs(ctx);
1404 ctx_new = ly_ctx_new_ylpath(searchpaths ? searchpaths[0] : NULL, ylpath, format, 0);
1405 free(ylpath);
1406 }
1407 } else {
1408create_empty:
1409#else
Radek Krejci92f5ce92019-09-06 16:25:43 +02001410 (void) arg; /* TODO yang-library support */
Radek Krejcied5acc52019-04-25 15:57:04 +02001411 {
1412#endif
1413 ly_ctx_new(NULL, options, &ctx_new);
1414 }
1415
1416 if (!ctx_new) {
1417 fprintf(stderr, "Failed to create context.\n");
1418 return 1;
1419 }
1420
1421 /* final switch */
1422 ly_ctx_destroy(ctx, NULL);
1423 ctx = ctx_new;
1424
1425 return 0;
1426}
1427
1428int
1429cmd_verb(const char *arg)
1430{
1431 const char *verb;
1432 if (strlen(arg) < 5) {
1433 cmd_verb_help();
1434 return 1;
1435 }
1436
1437 verb = arg + 5;
1438 if (!strcmp(verb, "error") || !strcmp(verb, "0")) {
1439 ly_verb(LY_LLERR);
1440#ifndef NDEBUG
1441 ly_verb_dbg(0);
1442#endif
1443 } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) {
1444 ly_verb(LY_LLWRN);
1445#ifndef NDEBUG
1446 ly_verb_dbg(0);
1447#endif
1448 } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) {
1449 ly_verb(LY_LLVRB);
1450#ifndef NDEBUG
1451 ly_verb_dbg(0);
1452#endif
1453 } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) {
1454 ly_verb(LY_LLDBG);
1455#ifndef NDEBUG
1456 ly_verb_dbg(LY_LDGDICT | LY_LDGYANG | LY_LDGYIN | LY_LDGXPATH | LY_LDGDIFF);
1457#endif
1458 } else {
1459 fprintf(stderr, "Unknown verbosity \"%s\"\n", verb);
1460 return 1;
1461 }
1462
1463 return 0;
1464}
1465
1466#ifndef NDEBUG
1467
1468int
1469cmd_debug(const char *arg)
1470{
1471 const char *beg, *end;
1472 int grps = 0;
1473 if (strlen(arg) < 6) {
1474 cmd_debug_help();
1475 return 1;
1476 }
1477
1478 end = arg + 6;
1479 while (end[0]) {
1480 for (beg = end; isspace(beg[0]); ++beg);
1481 if (!beg[0]) {
1482 break;
1483 }
1484
1485 for (end = beg; (end[0] && !isspace(end[0])); ++end);
1486
1487 if (!strncmp(beg, "dict", end - beg)) {
1488 grps |= LY_LDGDICT;
1489 } else if (!strncmp(beg, "yang", end - beg)) {
1490 grps |= LY_LDGYANG;
1491 } else if (!strncmp(beg, "yin", end - beg)) {
1492 grps |= LY_LDGYIN;
1493 } else if (!strncmp(beg, "xpath", end - beg)) {
1494 grps |= LY_LDGXPATH;
1495 } else if (!strncmp(beg, "diff", end - beg)) {
1496 grps |= LY_LDGDIFF;
1497 } else {
1498 fprintf(stderr, "Unknown debug group \"%.*s\"\n", (int)(end - beg), beg);
1499 return 1;
1500 }
1501 }
1502 ly_verb_dbg(grps);
1503
1504 return 0;
1505}
1506
1507#endif
1508
1509int
1510cmd_quit(const char *UNUSED(arg))
1511{
1512 done = 1;
1513 return 0;
1514}
1515
1516int
1517cmd_help(const char *arg)
1518{
1519 int i;
1520 char *args = strdup(arg);
1521 char *cmd = NULL;
1522
1523 strtok(args, " ");
1524 if ((cmd = strtok(NULL, " ")) == NULL) {
1525
1526generic_help:
1527 fprintf(stdout, "Available commands:\n");
1528
1529 for (i = 0; commands[i].name; i++) {
1530 if (commands[i].helpstring != NULL) {
1531 fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring);
1532 }
1533 }
1534 } else {
1535 /* print specific help for the selected command */
1536
1537 /* get the command of the specified name */
1538 for (i = 0; commands[i].name; i++) {
1539 if (strcmp(cmd, commands[i].name) == 0) {
1540 break;
1541 }
1542 }
1543
1544 /* execute the command's help if any valid command specified */
1545 if (commands[i].name) {
1546 if (commands[i].help_func != NULL) {
1547 commands[i].help_func();
1548 } else {
1549 printf("%s\n", commands[i].helpstring);
1550 }
1551 } else {
1552 /* if unknown command specified, print the list of commands */
1553 printf("Unknown command \'%s\'\n", cmd);
1554 goto generic_help;
1555 }
1556 }
1557
1558 free(args);
1559 return 0;
1560}
1561
1562COMMAND commands[] = {
1563 {"help", cmd_help, NULL, "Display commands description"},
1564 {"add", cmd_add, cmd_add_help, "Add a new model from a specific file"},
1565 {"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
1566 {"print", cmd_print, cmd_print_help, "Print a model"},
Radek Krejcied5acc52019-04-25 15:57:04 +02001567 {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
Radek Krejcie7b95092019-05-15 11:03:07 +02001568#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001569 {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"},
1570 {"list", cmd_list, cmd_list_help, "List all the loaded models"},
1571#endif
1572 {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"},
1573 {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"},
1574 {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"},
1575 {"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
1576#ifndef NDEBUG
1577 {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
1578#endif
1579 {"quit", cmd_quit, NULL, "Quit the program"},
1580 /* synonyms for previous commands */
1581 {"?", cmd_help, NULL, "Display commands description"},
1582 {"exit", cmd_quit, NULL, "Quit the program"},
1583 {NULL, NULL, NULL, NULL}
1584};