blob: 58ebe966dd2109c689e598ed8e584973062d1d72 [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;
177 /* TODO YIN parser not yet implemented
178 } else if (!strcmp(ptr, "yin")) {
179 return LYS_IN_YIN;
180 */
181 } else {
182 fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr);
183 return LYS_IN_UNKNOWN;
184 }
185 } else {
186 fprintf(stdout, "Input file \"%s\" without extension - unknown format.\n", path);
187 return LYS_IN_UNKNOWN;
188 }
189}
190
191int
192cmd_add(const char *arg)
193{
194 int path_len, ret = 1, index = 0;
195 char *path, *dir, *s, *arg_ptr;
196 const char * const *searchpaths;
197 const struct lys_module *model;
198 LYS_INFORMAT format = LYS_IN_UNKNOWN;
199
200 if (strlen(arg) < 5) {
201 cmd_add_help();
202 return 1;
203 }
204
205 arg_ptr = strdup(arg + 3 /* ignore "add" */);
206
207 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
208 if (s[2] == '\0' || s[2] == ' ') {
209 ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED);
210 s[0] = s[1] = ' ';
211 }
212 }
213 s = arg_ptr;
214
215 while (arg_ptr[0] == ' ') {
216 ++arg_ptr;
217 }
218 if (strchr(arg_ptr, ' ')) {
219 path_len = strchr(arg_ptr, ' ') - arg_ptr;
220 } else {
221 path_len = strlen(arg_ptr);
222 }
223 path = strndup(arg_ptr, path_len);
224
225 searchpaths = ly_ctx_get_searchdirs(ctx);
226 if (searchpaths) {
227 for (index = 0; searchpaths[index]; index++);
228 }
229
230 while (path) {
231 format = get_schema_format(path);
232 if (format == LYS_IN_UNKNOWN) {
233 free(path);
234 goto cleanup;
235 }
236
237 dir = strdup(path);
238 ly_ctx_set_searchdir(ctx, dirname(dir));
239 model = lys_parse_path(ctx, path, format);
240 ly_ctx_unset_searchdir(ctx, index);
241 free(path);
242 free(dir);
243
244 if (!model) {
245 /* libyang printed the error messages */
246 goto cleanup;
247 }
248
249 /* next model */
250 arg_ptr += path_len;
251 while (arg_ptr[0] == ' ') {
252 ++arg_ptr;
253 }
254 if (strchr(arg_ptr, ' ')) {
255 path_len = strchr(arg_ptr, ' ') - arg_ptr;
256 } else {
257 path_len = strlen(arg_ptr);
258 }
259
260 if (path_len) {
261 path = strndup(arg_ptr, path_len);
262 } else {
263 path = NULL;
264 }
265 }
266 if (format == LYS_IN_UNKNOWN) {
267 /* no schema on input */
268 cmd_add_help();
269 goto cleanup;
270 }
271 ret = 0;
272
273cleanup:
274 free(s);
275 ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED);
276
277 return ret;
278}
279
280int
281cmd_load(const char *arg)
282{
283 int name_len, ret = 1;
284 char *name, *s, *arg_ptr;
285 const struct lys_module *model;
286
287 if (strlen(arg) < 6) {
288 cmd_load_help();
289 return 1;
290 }
291
292 arg_ptr = strdup(arg + 4 /* ignore "load" */);
293
294 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
295 if (s[2] == '\0' || s[2] == ' ') {
296 ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED);
297 s[0] = s[1] = ' ';
298 }
299 }
300 s = arg_ptr;
301
302 while (arg_ptr[0] == ' ') {
303 ++arg_ptr;
304 }
305 if (strchr(arg_ptr, ' ')) {
306 name_len = strchr(arg_ptr, ' ') - arg_ptr;
307 } else {
308 name_len = strlen(arg_ptr);
309 }
310 name = strndup(arg_ptr, name_len);
311
312 while (name) {
313 model = ly_ctx_load_module(ctx, name, NULL);
314 free(name);
315 if (!model) {
316 /* libyang printed the error messages */
317 goto cleanup;
318 }
319
320 /* next model */
321 arg_ptr += name_len;
322 while (arg_ptr[0] == ' ') {
323 ++arg_ptr;
324 }
325 if (strchr(arg_ptr, ' ')) {
326 name_len = strchr(arg_ptr, ' ') - arg_ptr;
327 } else {
328 name_len = strlen(arg_ptr);
329 }
330
331 if (name_len) {
332 name = strndup(arg_ptr, name_len);
333 } else {
334 name = NULL;
335 }
336 }
337 ret = 0;
338
339cleanup:
340 free(s);
341 ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED);
342
343 return ret;
344}
345
346int
347cmd_print(const char *arg)
348{
Radek Krejci693262f2019-04-29 15:23:20 +0200349 int c, argc, option_index, ret = 1, tree_ll = 0, tree_opts = 0, compiled = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200350 char **argv = NULL, *ptr, *model_name, *revision;
351 const char *out_path = NULL;
352 const struct lys_module *module;
353 LYS_OUTFORMAT format = LYS_OUT_TREE;
354 FILE *output = stdout;
355 static struct option long_options[] = {
Radek Krejci693262f2019-04-29 15:23:20 +0200356 {"compiled", no_argument, 0, 'c'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200357 {"help", no_argument, 0, 'h'},
358 {"format", required_argument, 0, 'f'},
359 {"output", required_argument, 0, 'o'},
360#if 0
361 {"tree-print-groupings", no_argument, 0, 'g'},
362 {"tree-print-uses", no_argument, 0, 'u'},
363 {"tree-no-leafref-target", no_argument, 0, 'n'},
364 {"tree-path", required_argument, 0, 'P'},
365 {"info-path", required_argument, 0, 'P'},
366 {"tree-line-length", required_argument, 0, 'L'},
367#endif
368 {NULL, 0, 0, 0}
369 };
370 void *rlcd;
371
372 argc = 1;
373 argv = malloc(2*sizeof *argv);
374 *argv = strdup(arg);
375 ptr = strtok(*argv, " ");
376 while ((ptr = strtok(NULL, " "))) {
377 rlcd = realloc(argv, (argc+2)*sizeof *argv);
378 if (!rlcd) {
379 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
380 goto cleanup;
381 }
382 argv = rlcd;
383 argv[argc++] = ptr;
384 }
385 argv[argc] = NULL;
386
387 optind = 0;
388 while (1) {
389 option_index = 0;
Radek Krejci693262f2019-04-29 15:23:20 +0200390 c = getopt_long(argc, argv, "chf:go:guP:L:", long_options, &option_index);
Radek Krejcied5acc52019-04-25 15:57:04 +0200391 if (c == -1) {
392 break;
393 }
394
395 switch (c) {
Radek Krejci693262f2019-04-29 15:23:20 +0200396 case 'c':
397 compiled = 1;
398 break;
Radek Krejcied5acc52019-04-25 15:57:04 +0200399 case 'h':
400 cmd_print_help();
401 ret = 0;
402 goto cleanup;
403 case 'f':
404 if (!strcmp(optarg, "yang")) {
405 format = LYS_OUT_YANG;
406#if 0
407 } else if (!strcmp(optarg, "yin")) {
408 format = LYS_OUT_YIN;
409 } else if (!strcmp(optarg, "tree")) {
410 format = LYS_OUT_TREE;
411 } else if (!strcmp(optarg, "tree-rfc")) {
412 format = LYS_OUT_TREE;
413 tree_opts |= LYS_OUTOPT_TREE_RFC;
414 } else if (!strcmp(optarg, "info")) {
415 format = LYS_OUT_INFO;
416 } else if (!strcmp(optarg, "jsons")) {
417 format = LYS_OUT_JSON;
418#endif
419 } else {
420 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
421 goto cleanup;
422 }
423 break;
424 case 'o':
425 if (out_path) {
426 fprintf(stderr, "Output specified twice.\n");
427 goto cleanup;
428 }
429 out_path = optarg;
430 break;
431#if 0
432 case 'g':
433 tree_opts |= LYS_OUTOPT_TREE_GROUPING;
434 break;
435 case 'u':
436 tree_opts |= LYS_OUTOPT_TREE_USES;
437 break;
438 case 'n':
439 tree_opts |= LYS_OUTOPT_TREE_NO_LEAFREF;
440 break;
441 case 'P':
442 target_path = optarg;
443 break;
444 case 'L':
445 tree_ll = atoi(optarg);
446 break;
447#endif
448 case '?':
449 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
450 goto cleanup;
451 }
452 }
453
454 /* file name */
455 if (optind == argc) {
456 fprintf(stderr, "Missing the module name.\n");
457 goto cleanup;
458 }
459
Radek Krejci693262f2019-04-29 15:23:20 +0200460 /* compiled format */
461 if (compiled) {
462 format++;
463 }
Radek Krejci77954e82019-04-30 13:51:29 +0200464#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200465 /* tree fromat with or without gropings */
466 if ((tree_opts || tree_ll) && format != LYS_OUT_TREE) {
467 fprintf(stderr, "--tree options take effect only in case of the tree output format.\n");
468 }
Radek Krejci77954e82019-04-30 13:51:29 +0200469#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200470 /* module, revision */
471 model_name = argv[optind];
472 revision = NULL;
473 if (strchr(model_name, '@')) {
474 revision = strchr(model_name, '@');
475 revision[0] = '\0';
476 ++revision;
477 }
478
479 if (revision) {
480 module = ly_ctx_get_module(ctx, model_name, revision);
481 } else {
482 module = ly_ctx_get_module_latest(ctx, model_name);
483 }
484#if 0
485 if (!module) {
486 /* not a module, try to find it as a submodule */
487 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
488 }
489#endif
490
491 if (!module) {
492 if (revision) {
493 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
494 } else {
495 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
496 }
497 goto cleanup;
498 }
499
500 if (out_path) {
501 output = fopen(out_path, "w");
502 if (!output) {
503 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
504 goto cleanup;
505 }
506 }
507
508 ret = lys_print_file(output, module, format, tree_ll, tree_opts);
509 if (format == LYS_OUT_JSON) {
510 fputs("\n", output);
511 }
512
513cleanup:
514 free(*argv);
515 free(argv);
516
517 if (output && (output != stdout)) {
518 fclose(output);
519 }
520
521 return ret;
522}
523#if 0
524static LYD_FORMAT
525detect_data_format(char *filepath)
526{
527 size_t len;
528
529 /* detect input format according to file suffix */
530 len = strlen(filepath);
531 for (; isspace(filepath[len - 1]); len--, filepath[len] = '\0'); /* remove trailing whitespaces */
532 if (len >= 5 && !strcmp(&filepath[len - 4], ".xml")) {
533 return LYD_XML;
534 } else if (len >= 6 && !strcmp(&filepath[len - 5], ".json")) {
535 return LYD_JSON;
536 } else if (len >= 5 && !strcmp(&filepath[len - 4], ".lyb")) {
537 return LYD_LYB;
538 } else {
539 return LYD_UNKNOWN;
540 }
541}
542
543static int
544parse_data(char *filepath, int *options, struct lyd_node *val_tree, const char *rpc_act_file,
545 struct lyd_node **result)
546{
547 LYD_FORMAT informat = LYD_UNKNOWN;
548 struct lyxml_elem *xml = NULL;
549 struct lyd_node *data = NULL, *rpc_act = NULL;
550 int opts = *options;
551
552 /* detect input format according to file suffix */
553 informat = detect_data_format(filepath);
554 if (informat == LYD_UNKNOWN) {
555 fprintf(stderr, "Unable to resolve format of the input file, please add \".xml\", \".json\", or \".lyb\" suffix.\n");
556 return EXIT_FAILURE;
557 }
558
559 ly_errno = LY_SUCCESS;
560
561 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 {
636 if (opts & LYD_OPT_RPCREPLY) {
637 if (!rpc_act_file) {
638 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
639 return EXIT_FAILURE;
640 }
641 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
642 if (!rpc_act) {
643 fprintf(stderr, "Failed to parse RPC/action.\n");
644 return EXIT_FAILURE;
645 }
646 data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act, val_tree);
647 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
648 data = lyd_parse_path(ctx, filepath, informat, opts, val_tree);
649 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
650 if (!rpc_act_file) {
651 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
652 return EXIT_FAILURE;
653 }
654 data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file);
655 } else {
656 if (!(opts & LYD_OPT_TYPEMASK)) {
657 /* automatically add yang-library data */
658 opts |= LYD_OPT_DATA_ADD_YANGLIB;
659 }
660 data = lyd_parse_path(ctx, filepath, informat, opts);
661 }
662 }
663 lyd_free_withsiblings(rpc_act);
664
665 if (ly_errno) {
666 fprintf(stderr, "Failed to parse data.\n");
667 lyd_free_withsiblings(data);
668 return EXIT_FAILURE;
669 }
670
671 *result = data;
672 *options = opts;
673 return EXIT_SUCCESS;
674}
675
676int
677cmd_data(const char *arg)
678{
679 int c, argc, option_index, ret = 1;
680 int options = 0, printopt = 0;
681 char **argv = NULL, *ptr;
682 const char *out_path = NULL;
683 struct lyd_node *data = NULL, *val_tree = NULL;
684 LYD_FORMAT outformat = LYD_UNKNOWN;
685 FILE *output = stdout;
686 static struct option long_options[] = {
687 {"defaults", required_argument, 0, 'd'},
688 {"help", no_argument, 0, 'h'},
689 {"format", required_argument, 0, 'f'},
690 {"option", required_argument, 0, 't'},
691 {"output", required_argument, 0, 'o'},
692 {"running", required_argument, 0, 'r'},
693 {"strict", no_argument, 0, 's'},
694 {NULL, 0, 0, 0}
695 };
696 void *rlcd;
697
698 argc = 1;
699 argv = malloc(2*sizeof *argv);
700 *argv = strdup(arg);
701 ptr = strtok(*argv, " ");
702 while ((ptr = strtok(NULL, " "))) {
703 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
704 if (!rlcd) {
705 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
706 goto cleanup;
707 }
708 argv = rlcd;
709 argv[argc++] = ptr;
710 }
711 argv[argc] = NULL;
712
713 optind = 0;
714 while (1) {
715 option_index = 0;
716 c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index);
717 if (c == -1) {
718 break;
719 }
720
721 switch (c) {
722 case 'd':
723 if (!strcmp(optarg, "all")) {
724 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
725 } else if (!strcmp(optarg, "all-tagged")) {
726 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
727 } else if (!strcmp(optarg, "trim")) {
728 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;
729 } else if (!strcmp(optarg, "implicit-tagged")) {
730 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
731 }
732 break;
733 case 'h':
734 cmd_data_help();
735 ret = 0;
736 goto cleanup;
737 case 'f':
738 if (!strcmp(optarg, "xml")) {
739 outformat = LYD_XML;
740 } else if (!strcmp(optarg, "json")) {
741 outformat = LYD_JSON;
742 } else if (!strcmp(optarg, "lyb")) {
743 outformat = LYD_LYB;
744 } else {
745 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
746 goto cleanup;
747 }
748 break;
749 case 'o':
750 if (out_path) {
751 fprintf(stderr, "Output specified twice.\n");
752 goto cleanup;
753 }
754 out_path = optarg;
755 break;
756 case 'r':
757 if (val_tree || (options & LYD_OPT_NOEXTDEPS)) {
758 fprintf(stderr, "The running datastore (-r) cannot be set multiple times.\n");
759 goto cleanup;
760 }
761 if (optarg[0] == '!') {
762 /* ignore extenral dependencies to the running datastore */
763 options |= LYD_OPT_NOEXTDEPS;
764 } else {
765 /* external file with the running datastore */
766 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB);
767 if (!val_tree) {
768 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
769 goto cleanup;
770 }
771 }
772 break;
773 case 's':
774 options |= LYD_OPT_STRICT;
775 options |= LYD_OPT_OBSOLETE;
776 break;
777 case 't':
778 if (!strcmp(optarg, "auto")) {
779 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
780 } else if (!strcmp(optarg, "data")) {
781 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA;
782 } else if (!strcmp(optarg, "config")) {
783 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
784 } else if (!strcmp(optarg, "get")) {
785 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
786 } else if (!strcmp(optarg, "getconfig")) {
787 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
788 } else if (!strcmp(optarg, "edit")) {
789 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
790 } else if (!strcmp(optarg, "rpc")) {
791 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
792 } else if (!strcmp(optarg, "rpcreply")) {
793 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
794 } else if (!strcmp(optarg, "notif")) {
795 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
796 } else if (!strcmp(optarg, "yangdata")) {
797 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
798 } else {
799 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
800 cmd_data_help();
801 goto cleanup;
802 }
803 break;
804 case '?':
805 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
806 goto cleanup;
807 }
808 }
809
810 /* file name */
811 if (optind == argc) {
812 fprintf(stderr, "Missing the data file name.\n");
813 goto cleanup;
814 }
815
816 if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
817 goto cleanup;
818 }
819
820 if (out_path) {
821 output = fopen(out_path, "w");
822 if (!output) {
823 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
824 goto cleanup;
825 }
826 }
827
828 if (outformat != LYD_UNKNOWN) {
829 if (options & LYD_OPT_RPCREPLY) {
830 lyd_print_file(output, data->child, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt);
831 } else {
832 lyd_print_file(output, data, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt);
833 }
834 }
835
836 ret = 0;
837
838cleanup:
839 free(*argv);
840 free(argv);
841
842 if (output && (output != stdout)) {
843 fclose(output);
844 }
845
846 lyd_free_withsiblings(val_tree);
847 lyd_free_withsiblings(data);
848
849 return ret;
850}
851
852int
853cmd_xpath(const char *arg)
854{
855 int c, argc, option_index, ret = 1, long_str;
856 char **argv = NULL, *ptr, *expr = NULL;
857 unsigned int i, j;
858 int options = 0;
859 struct lyd_node *data = NULL, *node, *val_tree = NULL;
860 struct lyd_node_leaf_list *key;
861 struct ly_set *set;
862 static struct option long_options[] = {
863 {"help", no_argument, 0, 'h'},
864 {"expr", required_argument, 0, 'e'},
865 {NULL, 0, 0, 0}
866 };
867 void *rlcd;
868
869 long_str = 0;
870 argc = 1;
871 argv = malloc(2 * sizeof *argv);
872 *argv = strdup(arg);
873 ptr = strtok(*argv, " ");
874 while ((ptr = strtok(NULL, " "))) {
875 if (long_str) {
876 ptr[-1] = ' ';
877 if (ptr[strlen(ptr) - 1] == long_str) {
878 long_str = 0;
879 ptr[strlen(ptr) - 1] = '\0';
880 }
881 } else {
882 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
883 if (!rlcd) {
884 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
885 goto cleanup;
886 }
887 argv = rlcd;
888 argv[argc] = ptr;
889 if (ptr[0] == '"') {
890 long_str = '"';
891 ++argv[argc];
892 }
893 if (ptr[0] == '\'') {
894 long_str = '\'';
895 ++argv[argc];
896 }
897 if (ptr[strlen(ptr) - 1] == long_str) {
898 long_str = 0;
899 ptr[strlen(ptr) - 1] = '\0';
900 }
901 ++argc;
902 }
903 }
904 argv[argc] = NULL;
905
906 optind = 0;
907 while (1) {
908 option_index = 0;
909 c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index);
910 if (c == -1) {
911 break;
912 }
913
914 switch (c) {
915 case 'h':
916 cmd_xpath_help();
917 ret = 0;
918 goto cleanup;
919 case 'e':
920 expr = optarg;
921 break;
922 case 't':
923 if (!strcmp(optarg, "auto")) {
924 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
925 } else if (!strcmp(optarg, "config")) {
926 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
927 } else if (!strcmp(optarg, "get")) {
928 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
929 } else if (!strcmp(optarg, "getconfig")) {
930 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
931 } else if (!strcmp(optarg, "edit")) {
932 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
933 } else if (!strcmp(optarg, "rpc")) {
934 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
935 } else if (!strcmp(optarg, "rpcreply")) {
936 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
937 } else if (!strcmp(optarg, "notif")) {
938 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
939 } else if (!strcmp(optarg, "yangdata")) {
940 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
941 } else {
942 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
943 cmd_data_help();
944 goto cleanup;
945 }
946 break;
947 case 'x':
948 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG);
949 if (!val_tree) {
950 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
951 goto cleanup;
952 }
953 break;
954 case '?':
955 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
956 goto cleanup;
957 }
958 }
959
960 if (optind == argc) {
961 fprintf(stderr, "Missing the file with data.\n");
962 goto cleanup;
963 }
964
965 if (!expr) {
966 fprintf(stderr, "Missing the XPath expression.\n");
967 goto cleanup;
968 }
969
970 if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
971 goto cleanup;
972 }
973
974 if (!(set = lyd_find_path(data, expr))) {
975 goto cleanup;
976 }
977
978 /* print result */
979 printf("Result:\n");
980 if (!set->number) {
981 printf("\tEmpty\n");
982 } else {
983 for (i = 0; i < set->number; ++i) {
984 node = set->set.d[i];
985 switch (node->schema->nodetype) {
986 case LYS_CONTAINER:
987 printf("\tContainer ");
988 break;
989 case LYS_LEAF:
990 printf("\tLeaf ");
991 break;
992 case LYS_LEAFLIST:
993 printf("\tLeaflist ");
994 break;
995 case LYS_LIST:
996 printf("\tList ");
997 break;
998 case LYS_ANYXML:
999 printf("\tAnyxml ");
1000 break;
1001 case LYS_ANYDATA:
1002 printf("\tAnydata ");
1003 break;
1004 default:
1005 printf("\tUnknown ");
1006 break;
1007 }
1008 printf("\"%s\"", node->schema->name);
1009 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1010 printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str);
1011 } else if (node->schema->nodetype == LYS_LIST) {
1012 key = (struct lyd_node_leaf_list *)node->child;
1013 printf(" (");
1014 for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) {
1015 if (j) {
1016 printf(" ");
1017 }
1018 printf("\"%s\": %s", key->schema->name, key->value_str);
1019 key = (struct lyd_node_leaf_list *)key->next;
1020 }
1021 printf(")");
1022 }
1023 printf("\n");
1024 }
1025 }
1026 printf("\n");
1027
1028 ly_set_free(set);
1029 ret = 0;
1030
1031cleanup:
1032 free(*argv);
1033 free(argv);
1034
1035 lyd_free_withsiblings(data);
1036
1037 return ret;
1038}
1039
1040int
1041print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
1042{
1043 struct lyd_node *ylib;
1044 uint32_t idx = 0, has_modules = 0;
1045 uint8_t u;
1046 const struct lys_module *mod;
1047
1048 if (outformat != LYD_UNKNOWN) {
1049 ylib = ly_ctx_info(ctx);
1050 if (!ylib) {
1051 fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n");
1052 return 1;
1053 }
1054
1055 lyd_print_file(out, ylib, outformat, LYP_WITHSIBLINGS | LYP_FORMAT);
1056 lyd_free_withsiblings(ylib);
1057 return 0;
1058 }
1059
1060 /* iterate schemas in context and provide just the basic info */
1061 fprintf(out, "List of the loaded models:\n");
1062 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
1063 has_modules++;
1064
1065 /* conformance print */
1066 if (mod->implemented) {
1067 fprintf(out, "\tI");
1068 } else {
1069 fprintf(out, "\ti");
1070 }
1071
1072 /* module print */
1073 fprintf(out, " %s", mod->name);
1074 if (mod->rev_size) {
1075 fprintf(out, "@%s", mod->rev[0].date);
1076 }
1077
1078 /* submodules print */
1079 if (mod->inc_size) {
1080 fprintf(out, " (");
1081 for (u = 0; u < mod->inc_size; u++) {
1082 fprintf(out, "%s%s", !u ? "" : ",", mod->inc[u].submodule->name);
1083 if (mod->inc[u].submodule->rev_size) {
1084 fprintf(out, "@%s", mod->inc[u].submodule->rev[0].date);
1085 }
1086 }
1087 fprintf(out, ")");
1088 }
1089
1090 /* finish the line */
1091 fprintf(out, "\n");
1092 }
1093
1094 if (!has_modules) {
1095 fprintf(out, "\t(none)\n");
1096 }
1097
1098 return 0;
1099}
1100
1101int
1102cmd_list(const char *arg)
1103{
1104 char **argv = NULL, *ptr;
1105 int c, argc, option_index;
1106 LYD_FORMAT outformat = LYD_UNKNOWN;
1107 static struct option long_options[] = {
1108 {"help", no_argument, 0, 'h'},
1109 {"format", required_argument, 0, 'f'},
1110 {NULL, 0, 0, 0}
1111 };
1112 void *rlcd;
1113
1114 argc = 1;
1115 argv = malloc(2*sizeof *argv);
1116 *argv = strdup(arg);
1117 ptr = strtok(*argv, " ");
1118 while ((ptr = strtok(NULL, " "))) {
1119 rlcd = realloc(argv, (argc+2)*sizeof *argv);
1120 if (!rlcd) {
1121 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1122 goto error;
1123 }
1124 argv = rlcd;
1125 argv[argc++] = ptr;
1126 }
1127 argv[argc] = NULL;
1128
1129 optind = 0;
1130 while (1) {
1131 option_index = 0;
1132 c = getopt_long(argc, argv, "hf:", long_options, &option_index);
1133 if (c == -1) {
1134 break;
1135 }
1136
1137 switch (c) {
1138 case 'h':
1139 cmd_data_help();
1140 free(*argv);
1141 free(argv);
1142 return 0;
1143 case 'f':
1144 if (!strcmp(optarg, "xml")) {
1145 outformat = LYD_XML;
1146 } else if (!strcmp(optarg, "json")) {
1147 outformat = LYD_JSON;
1148 } else {
1149 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
1150 goto error;
1151 }
1152 break;
1153 case '?':
1154 /* getopt_long() prints message */
1155 goto error;
1156 }
1157 }
1158 if (optind != argc) {
1159 fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]);
1160error:
1161 free(*argv);
1162 free(argv);
1163 return 1;
1164 }
1165 free(*argv);
1166 free(argv);
1167
1168 return print_list(stdout, ctx, outformat);
1169}
1170#endif
1171int
1172cmd_feature(const char *arg)
1173{
1174 int c, argc, option_index, ret = 1, task = 0;
1175 char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL;
1176 const struct lys_module *module;
1177 static struct option long_options[] = {
1178 {"help", no_argument, 0, 'h'},
1179 {"enable", required_argument, 0, 'e'},
1180 {"disable", required_argument, 0, 'd'},
1181 {NULL, 0, 0, 0}
1182 };
1183 void *rlcd;
1184
1185 argc = 1;
1186 argv = malloc(2*sizeof *argv);
1187 *argv = strdup(arg);
1188 ptr = strtok(*argv, " ");
1189 while ((ptr = strtok(NULL, " "))) {
1190 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
1191 if (!rlcd) {
1192 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1193 goto cleanup;
1194 }
1195 argv = rlcd;
1196 argv[argc++] = ptr;
1197 }
1198 argv[argc] = NULL;
1199
1200 optind = 0;
1201 while (1) {
1202 option_index = 0;
1203 c = getopt_long(argc, argv, "he:d:", long_options, &option_index);
1204 if (c == -1) {
1205 break;
1206 }
1207
1208 switch (c) {
1209 case 'h':
1210 cmd_feature_help();
1211 ret = 0;
1212 goto cleanup;
1213 case 'e':
1214 if (task) {
1215 fprintf(stderr, "Only one of enable or disable can be specified.\n");
1216 goto cleanup;
1217 }
1218 task = 1;
1219 feat_names = optarg;
1220 break;
1221 case 'd':
1222 if (task) {
1223 fprintf(stderr, "Only one of enable, or disable can be specified.\n");
1224 goto cleanup;
1225 }
1226 task = 2;
1227 feat_names = optarg;
1228 break;
1229 case '?':
1230 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
1231 goto cleanup;
1232 }
1233 }
1234
1235 /* module name */
1236 if (optind == argc) {
1237 fprintf(stderr, "Missing the module name.\n");
1238 goto cleanup;
1239 }
1240
1241 revision = NULL;
1242 model_name = argv[optind];
1243 if (strchr(model_name, '@')) {
1244 revision = strchr(model_name, '@');
1245 revision[0] = '\0';
1246 ++revision;
1247 }
1248
1249 module = ly_ctx_get_module(ctx, model_name, revision);
1250#if 0
1251 if (!module) {
1252 /* not a module, try to find it as a submodule */
1253 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
1254 }
1255#endif
1256
1257 if (module == NULL) {
1258 if (revision) {
1259 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
1260 } else {
1261 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
1262 }
1263 goto cleanup;
1264 }
1265
1266 if (!task) {
1267 unsigned int len, max_len = 0;
1268 unsigned int u;
1269 struct lysc_feature *features;
1270
1271 printf("%s features:\n", module->name);
1272
1273 if (module->compiled) {
1274 features = module->compiled->features;
1275 } else {
1276 features = module->off_features;
1277 }
1278
1279 /* get the max len */
1280 LY_ARRAY_FOR(features, u) {
1281 len = strlen(features[u].name);
1282 if (len > max_len) {
1283 max_len = len;
1284 }
1285 }
1286
1287 LY_ARRAY_FOR(features, u) {
1288 printf("\t%-*s (%s)\n", max_len, features[u].name, (features[u].flags & LYS_FENABLED) ? "on" : "off");
1289 }
1290 if (!u) {
1291 printf("\t(none)\n");
1292 }
1293 } else {
1294 feat_names = strtok(feat_names, ",");
1295 while (feat_names) {
1296 if (((task == 1) && lys_feature_enable(module, feat_names))
1297 || ((task == 2) && lys_feature_disable(module, feat_names))) {
1298 fprintf(stderr, "Feature \"%s\" not found.\n", feat_names);
1299 ret = 1;
1300 }
1301 feat_names = strtok(NULL, ",");
1302 }
1303 }
1304
1305cleanup:
1306 free(*argv);
1307 free(argv);
1308
1309 return ret;
1310}
1311
1312int
1313cmd_searchpath(const char *arg)
1314{
1315 const char *path;
1316 const char * const *searchpaths;
1317 int index;
1318 struct stat st;
1319
1320 for (path = strchr(arg, ' '); path && (path[0] == ' '); ++path);
1321 if (!path || (path[0] == '\0')) {
1322 searchpaths = ly_ctx_get_searchdirs(ctx);
1323 if (searchpaths) {
1324 for (index = 0; searchpaths[index]; index++) {
1325 fprintf(stdout, "%s\n", searchpaths[index]);
1326 }
1327 }
1328 return 0;
1329 }
1330
1331 if ((!strncmp(path, "-h", 2) && (path[2] == '\0' || path[2] == ' ')) ||
1332 (!strncmp(path, "--help", 6) && (path[6] == '\0' || path[6] == ' '))) {
1333 cmd_searchpath_help();
1334 return 0;
1335 } else if (!strncmp(path, "--clear", 7) && (path[7] == '\0' || path[7] == ' ')) {
1336 ly_ctx_unset_searchdirs(ctx, NULL);
1337 return 0;
1338 }
1339
1340 if (stat(path, &st) == -1) {
1341 fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno));
1342 return 1;
1343 }
1344 if (!S_ISDIR(st.st_mode)) {
1345 fprintf(stderr, "\"%s\" is not a directory.\n", path);
1346 return 1;
1347 }
1348
1349 ly_ctx_set_searchdir(ctx, path);
1350
1351 return 0;
1352}
1353
1354int
1355cmd_clear(const char *arg)
1356{
1357 struct ly_ctx *ctx_new;
1358 int options = 0;
1359#if 0
1360 int i;
1361 char *ylpath;
1362 const char * const *searchpaths;
1363 LYD_FORMAT format;
1364
1365 /* get optional yang library file name */
1366 for (i = 5; arg[i] && isspace(arg[i]); i++);
1367 if (arg[i]) {
1368 if (arg[i] == '-' && arg[i + 1] == 'e') {
1369 options = LY_CTX_NOYANGLIBRARY;
1370 goto create_empty;
1371 } else {
1372 ylpath = strdup(&arg[i]);
1373 format = detect_data_format(ylpath);
1374 if (format == LYD_UNKNOWN) {
1375 free(ylpath);
1376 fprintf(stderr, "Unable to resolve format of the yang library file, please add \".xml\" or \".json\" suffix.\n");
1377 goto create_empty;
1378 }
1379 searchpaths = ly_ctx_get_searchdirs(ctx);
1380 ctx_new = ly_ctx_new_ylpath(searchpaths ? searchpaths[0] : NULL, ylpath, format, 0);
1381 free(ylpath);
1382 }
1383 } else {
1384create_empty:
1385#else
1386 (void) arg; /* TODO unused */
1387 {
1388#endif
1389 ly_ctx_new(NULL, options, &ctx_new);
1390 }
1391
1392 if (!ctx_new) {
1393 fprintf(stderr, "Failed to create context.\n");
1394 return 1;
1395 }
1396
1397 /* final switch */
1398 ly_ctx_destroy(ctx, NULL);
1399 ctx = ctx_new;
1400
1401 return 0;
1402}
1403
1404int
1405cmd_verb(const char *arg)
1406{
1407 const char *verb;
1408 if (strlen(arg) < 5) {
1409 cmd_verb_help();
1410 return 1;
1411 }
1412
1413 verb = arg + 5;
1414 if (!strcmp(verb, "error") || !strcmp(verb, "0")) {
1415 ly_verb(LY_LLERR);
1416#ifndef NDEBUG
1417 ly_verb_dbg(0);
1418#endif
1419 } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) {
1420 ly_verb(LY_LLWRN);
1421#ifndef NDEBUG
1422 ly_verb_dbg(0);
1423#endif
1424 } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) {
1425 ly_verb(LY_LLVRB);
1426#ifndef NDEBUG
1427 ly_verb_dbg(0);
1428#endif
1429 } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) {
1430 ly_verb(LY_LLDBG);
1431#ifndef NDEBUG
1432 ly_verb_dbg(LY_LDGDICT | LY_LDGYANG | LY_LDGYIN | LY_LDGXPATH | LY_LDGDIFF);
1433#endif
1434 } else {
1435 fprintf(stderr, "Unknown verbosity \"%s\"\n", verb);
1436 return 1;
1437 }
1438
1439 return 0;
1440}
1441
1442#ifndef NDEBUG
1443
1444int
1445cmd_debug(const char *arg)
1446{
1447 const char *beg, *end;
1448 int grps = 0;
1449 if (strlen(arg) < 6) {
1450 cmd_debug_help();
1451 return 1;
1452 }
1453
1454 end = arg + 6;
1455 while (end[0]) {
1456 for (beg = end; isspace(beg[0]); ++beg);
1457 if (!beg[0]) {
1458 break;
1459 }
1460
1461 for (end = beg; (end[0] && !isspace(end[0])); ++end);
1462
1463 if (!strncmp(beg, "dict", end - beg)) {
1464 grps |= LY_LDGDICT;
1465 } else if (!strncmp(beg, "yang", end - beg)) {
1466 grps |= LY_LDGYANG;
1467 } else if (!strncmp(beg, "yin", end - beg)) {
1468 grps |= LY_LDGYIN;
1469 } else if (!strncmp(beg, "xpath", end - beg)) {
1470 grps |= LY_LDGXPATH;
1471 } else if (!strncmp(beg, "diff", end - beg)) {
1472 grps |= LY_LDGDIFF;
1473 } else {
1474 fprintf(stderr, "Unknown debug group \"%.*s\"\n", (int)(end - beg), beg);
1475 return 1;
1476 }
1477 }
1478 ly_verb_dbg(grps);
1479
1480 return 0;
1481}
1482
1483#endif
1484
1485int
1486cmd_quit(const char *UNUSED(arg))
1487{
1488 done = 1;
1489 return 0;
1490}
1491
1492int
1493cmd_help(const char *arg)
1494{
1495 int i;
1496 char *args = strdup(arg);
1497 char *cmd = NULL;
1498
1499 strtok(args, " ");
1500 if ((cmd = strtok(NULL, " ")) == NULL) {
1501
1502generic_help:
1503 fprintf(stdout, "Available commands:\n");
1504
1505 for (i = 0; commands[i].name; i++) {
1506 if (commands[i].helpstring != NULL) {
1507 fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring);
1508 }
1509 }
1510 } else {
1511 /* print specific help for the selected command */
1512
1513 /* get the command of the specified name */
1514 for (i = 0; commands[i].name; i++) {
1515 if (strcmp(cmd, commands[i].name) == 0) {
1516 break;
1517 }
1518 }
1519
1520 /* execute the command's help if any valid command specified */
1521 if (commands[i].name) {
1522 if (commands[i].help_func != NULL) {
1523 commands[i].help_func();
1524 } else {
1525 printf("%s\n", commands[i].helpstring);
1526 }
1527 } else {
1528 /* if unknown command specified, print the list of commands */
1529 printf("Unknown command \'%s\'\n", cmd);
1530 goto generic_help;
1531 }
1532 }
1533
1534 free(args);
1535 return 0;
1536}
1537
1538COMMAND commands[] = {
1539 {"help", cmd_help, NULL, "Display commands description"},
1540 {"add", cmd_add, cmd_add_help, "Add a new model from a specific file"},
1541 {"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
1542 {"print", cmd_print, cmd_print_help, "Print a model"},
1543#if 0
1544 {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
1545 {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"},
1546 {"list", cmd_list, cmd_list_help, "List all the loaded models"},
1547#endif
1548 {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"},
1549 {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"},
1550 {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"},
1551 {"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
1552#ifndef NDEBUG
1553 {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
1554#endif
1555 {"quit", cmd_quit, NULL, "Quit the program"},
1556 /* synonyms for previous commands */
1557 {"?", cmd_help, NULL, "Display commands description"},
1558 {"exit", cmd_quit, NULL, "Quit the program"},
1559 {NULL, NULL, NULL, NULL}
1560};