blob: fbafa1acf2d0c5f8c3cdbb518b04463f6857b6ee [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcied5acc52019-04-25 15:57:04 +020016
17#include "commands.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020018
19#include <ctype.h>
20#include <errno.h>
21#include <getopt.h>
22#include <libgen.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejcied5acc52019-04-25 15:57:04 +020029#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{
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080061 printf("print [-f (yang | yin | tree [<tree-options>] | info [-P <info-path>] [-(-s)ingle-node])] [-o <output-file>]"
Radek Krejcied5acc52019-04-25 15:57:04 +020062 " <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");
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080071 printf("\tinfo-path:\t<schema-path> | identity/<identity-name> | feature/<feature-name>\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020072 printf("\n");
73 printf("\tschema-path:\t( /<module-name>:<node-identifier> )+\n");
74}
75
76void
77cmd_data_help(void)
78{
79 printf("data [-(-s)trict] [-t TYPE] [-d DEFAULTS] [-o <output-file>] [-f (xml | json | lyb)] [-r <running-file-name>]\n");
80 printf(" <data-file-name> [<RPC/action-data-file-name> | <yang-data name>]\n\n");
81 printf("Accepted TYPEs:\n");
82 printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n");
83 printf("\t this option is applicable only in case of XML input data.\n");
84 printf("\tdata - LYD_OPT_DATA (default value) - complete datastore including status data.\n");
85 printf("\tconfig - LYD_OPT_CONFIG - complete configuration datastore.\n");
86 printf("\tget - LYD_OPT_GET - <get> operation result.\n");
87 printf("\tgetconfig - LYD_OPT_GETCONFIG - <get-config> operation result.\n");
88 printf("\tedit - LYD_OPT_EDIT - <edit-config>'s data (content of its <config> element).\n");
89 printf("\trpc - LYD_OPT_RPC - NETCONF RPC message.\n");
90 printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n");
91 printf("\tnotif - LYD_OPT_NOTIF - NETCONF Notification message.\n");
92 printf("\tyangdata - LYD_OPT_DATA_TEMPLATE - yang-data extension (last parameter mandatory in this case)\n\n");
93 printf("Accepted DEFAULTS:\n");
94 printf("\tall - add missing default nodes\n");
95 printf("\tall-tagged - add missing default nodes and mark all the default nodes with the attribute.\n");
96 printf("\ttrim - remove all nodes with a default value\n");
97 printf("\timplicit-tagged - add missing nodes and mark them with the attribute\n\n");
98 printf("Option -r:\n");
99 printf("\tOptional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the file contains running\n");
100 printf("\tconfiguration datastore data referenced from the RPC/Notification. Note that the file is\n");
101 printf("\tvalidated as 'data' TYPE. Special value '!' can be used as argument to ignore the\n");
102 printf("\texternal references.\n\n");
103 printf("\tIf an XPath expression (when/must) needs access to configuration data, you can provide\n");
104 printf("\tthem in a file, which will be parsed as 'data' TYPE.\n\n");
105}
106
107void
108cmd_xpath_help(void)
109{
110 printf("xpath [-t TYPE] [-x <additional-tree-file-name>] -e <XPath-expression>\n"
111 " <XML-data-file-name> [<JSON-rpc/action-schema-nodeid>]\n");
112 printf("Accepted TYPEs:\n");
113 printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n");
114 printf("\t this option is applicable only in case of XML input data.\n");
115 printf("\tconfig - LYD_OPT_CONFIG\n");
116 printf("\tget - LYD_OPT_GET\n");
117 printf("\tgetconfig - LYD_OPT_GETCONFIG\n");
118 printf("\tedit - LYD_OPT_EDIT\n");
119 printf("\trpc - LYD_OPT_RPC\n");
120 printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n");
121 printf("\tnotif - LYD_OPT_NOTIF\n\n");
122 printf("Option -x:\n");
123 printf("\tIf RPC/action/notification/RPC reply (for TYPEs 'rpc', 'rpcreply', and 'notif') includes\n");
124 printf("\tan XPath expression (when/must) that needs access to the configuration data, you can provide\n");
125 printf("\tthem in a file, which will be parsed as 'config'.\n");
126}
127
128void
129cmd_list_help(void)
130{
131 printf("list [-f (xml | json)]\n\n");
132 printf("\tBasic list output (no -f): i - imported module, I - implemented module\n");
133}
134
135void
136cmd_feature_help(void)
137{
138 printf("feature [ -(-e)nable | -(-d)isable (* | <feature-name>[,<feature-name> ...]) ] <model-name>[@<revision>]\n");
139}
140
141void
142cmd_searchpath_help(void)
143{
144 printf("searchpath [<model-dir-path> | --clear]\n\n");
145 printf("\tThey are used to search for imports and includes of a model.\n");
146 printf("\tThe \"load\" command uses these directories to find models directly.\n");
147}
148
149void
150cmd_verb_help(void)
151{
152 printf("verb (error/0 | warning/1 | verbose/2 | debug/3)\n");
153}
154
155#ifndef NDEBUG
156
157void
158cmd_debug_help(void)
159{
160 printf("debug (dict | yang | yin | xpath | diff)+\n");
161}
162
163#endif
164
165LYS_INFORMAT
166get_schema_format(const char *path)
167{
168 char *ptr;
169
170 if ((ptr = strrchr(path, '.')) != NULL) {
171 ++ptr;
172 if (!strcmp(ptr, "yang")) {
173 return LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200174 } else if (!strcmp(ptr, "yin")) {
175 return LYS_IN_YIN;
Radek Krejcied5acc52019-04-25 15:57:04 +0200176 } else {
177 fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr);
178 return LYS_IN_UNKNOWN;
179 }
180 } else {
181 fprintf(stdout, "Input file \"%s\" without extension - unknown format.\n", path);
182 return LYS_IN_UNKNOWN;
183 }
184}
185
186int
187cmd_add(const char *arg)
188{
189 int path_len, ret = 1, index = 0;
190 char *path, *dir, *s, *arg_ptr;
191 const char * const *searchpaths;
192 const struct lys_module *model;
193 LYS_INFORMAT format = LYS_IN_UNKNOWN;
194
195 if (strlen(arg) < 5) {
196 cmd_add_help();
197 return 1;
198 }
199
200 arg_ptr = strdup(arg + 3 /* ignore "add" */);
201
202 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
203 if (s[2] == '\0' || s[2] == ' ') {
Radek Krejci3fa46b62019-09-11 10:47:30 +0200204 ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200205 s[0] = s[1] = ' ';
206 }
207 }
208 s = arg_ptr;
209
210 while (arg_ptr[0] == ' ') {
211 ++arg_ptr;
212 }
213 if (strchr(arg_ptr, ' ')) {
214 path_len = strchr(arg_ptr, ' ') - arg_ptr;
215 } else {
216 path_len = strlen(arg_ptr);
217 }
218 path = strndup(arg_ptr, path_len);
219
220 searchpaths = ly_ctx_get_searchdirs(ctx);
221 if (searchpaths) {
222 for (index = 0; searchpaths[index]; index++);
223 }
224
225 while (path) {
226 format = get_schema_format(path);
227 if (format == LYS_IN_UNKNOWN) {
228 free(path);
229 goto cleanup;
230 }
231
232 dir = strdup(path);
233 ly_ctx_set_searchdir(ctx, dirname(dir));
234 model = lys_parse_path(ctx, path, format);
235 ly_ctx_unset_searchdir(ctx, index);
236 free(path);
237 free(dir);
238
239 if (!model) {
240 /* libyang printed the error messages */
241 goto cleanup;
242 }
243
244 /* next model */
245 arg_ptr += path_len;
246 while (arg_ptr[0] == ' ') {
247 ++arg_ptr;
248 }
249 if (strchr(arg_ptr, ' ')) {
250 path_len = strchr(arg_ptr, ' ') - arg_ptr;
251 } else {
252 path_len = strlen(arg_ptr);
253 }
254
255 if (path_len) {
256 path = strndup(arg_ptr, path_len);
257 } else {
258 path = NULL;
259 }
260 }
261 if (format == LYS_IN_UNKNOWN) {
262 /* no schema on input */
263 cmd_add_help();
264 goto cleanup;
265 }
266 ret = 0;
267
268cleanup:
269 free(s);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200270 ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200271
272 return ret;
273}
274
275int
276cmd_load(const char *arg)
277{
278 int name_len, ret = 1;
279 char *name, *s, *arg_ptr;
280 const struct lys_module *model;
281
282 if (strlen(arg) < 6) {
283 cmd_load_help();
284 return 1;
285 }
286
287 arg_ptr = strdup(arg + 4 /* ignore "load" */);
288
289 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
290 if (s[2] == '\0' || s[2] == ' ') {
Radek Krejci3fa46b62019-09-11 10:47:30 +0200291 ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200292 s[0] = s[1] = ' ';
293 }
294 }
295 s = arg_ptr;
296
297 while (arg_ptr[0] == ' ') {
298 ++arg_ptr;
299 }
300 if (strchr(arg_ptr, ' ')) {
301 name_len = strchr(arg_ptr, ' ') - arg_ptr;
302 } else {
303 name_len = strlen(arg_ptr);
304 }
305 name = strndup(arg_ptr, name_len);
306
307 while (name) {
308 model = ly_ctx_load_module(ctx, name, NULL);
309 free(name);
310 if (!model) {
311 /* libyang printed the error messages */
312 goto cleanup;
313 }
314
315 /* next model */
316 arg_ptr += name_len;
317 while (arg_ptr[0] == ' ') {
318 ++arg_ptr;
319 }
320 if (strchr(arg_ptr, ' ')) {
321 name_len = strchr(arg_ptr, ' ') - arg_ptr;
322 } else {
323 name_len = strlen(arg_ptr);
324 }
325
326 if (name_len) {
327 name = strndup(arg_ptr, name_len);
328 } else {
329 name = NULL;
330 }
331 }
332 ret = 0;
333
334cleanup:
335 free(s);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200336 ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200337
338 return ret;
339}
340
341int
342cmd_print(const char *arg)
343{
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800344 int c, argc, option_index, ret = 1, tree_ll = 0, output_opts = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200345 char **argv = NULL, *ptr, *model_name, *revision;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800346 const char *out_path = NULL, *target_path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200347 const struct lys_module *module;
348 LYS_OUTFORMAT format = LYS_OUT_TREE;
Radek Krejci241f6b52020-05-21 18:13:49 +0200349 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200350 static struct option long_options[] = {
351 {"help", no_argument, 0, 'h'},
352 {"format", required_argument, 0, 'f'},
353 {"output", required_argument, 0, 'o'},
354#if 0
355 {"tree-print-groupings", no_argument, 0, 'g'},
356 {"tree-print-uses", no_argument, 0, 'u'},
357 {"tree-no-leafref-target", no_argument, 0, 'n'},
358 {"tree-path", required_argument, 0, 'P'},
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800359#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200360 {"info-path", required_argument, 0, 'P'},
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800361 {"single-node", no_argument, 0, 's'},
362#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200363 {"tree-line-length", required_argument, 0, 'L'},
364#endif
365 {NULL, 0, 0, 0}
366 };
367 void *rlcd;
368
369 argc = 1;
370 argv = malloc(2*sizeof *argv);
371 *argv = strdup(arg);
372 ptr = strtok(*argv, " ");
373 while ((ptr = strtok(NULL, " "))) {
374 rlcd = realloc(argv, (argc+2)*sizeof *argv);
375 if (!rlcd) {
376 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
377 goto cleanup;
378 }
379 argv = rlcd;
380 argv[argc++] = ptr;
381 }
382 argv[argc] = NULL;
383
384 optind = 0;
385 while (1) {
386 option_index = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800387 c = getopt_long(argc, argv, "chf:go:guP:sL:", long_options, &option_index);
Radek Krejcied5acc52019-04-25 15:57:04 +0200388 if (c == -1) {
389 break;
390 }
391
392 switch (c) {
393 case 'h':
394 cmd_print_help();
395 ret = 0;
396 goto cleanup;
397 case 'f':
398 if (!strcmp(optarg, "yang")) {
399 format = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200400 } else if (!strcmp(optarg, "yin")) {
401 format = LYS_OUT_YIN;
FredGand944bdc2019-11-05 21:57:07 +0800402#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200403 } else if (!strcmp(optarg, "tree")) {
404 format = LYS_OUT_TREE;
405 } else if (!strcmp(optarg, "tree-rfc")) {
406 format = LYS_OUT_TREE;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800407 output_opts |= LYS_OUTOPT_TREE_RFC;
Radek Krejcied5acc52019-04-25 15:57:04 +0200408#endif
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800409 } else if (!strcmp(optarg, "info")) {
410 format = LYS_OUT_YANG_COMPILED;
Radek Krejcied5acc52019-04-25 15:57:04 +0200411 } else {
412 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
413 goto cleanup;
414 }
415 break;
416 case 'o':
417 if (out_path) {
418 fprintf(stderr, "Output specified twice.\n");
419 goto cleanup;
420 }
421 out_path = optarg;
422 break;
423#if 0
424 case 'g':
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800425 output_opts |= LYS_OUTOPT_TREE_GROUPING;
Radek Krejcied5acc52019-04-25 15:57:04 +0200426 break;
427 case 'u':
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800428 output_opts |= LYS_OUTOPT_TREE_USES;
Radek Krejcied5acc52019-04-25 15:57:04 +0200429 break;
430 case 'n':
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800431 output_opts |= LYS_OUTOPT_TREE_NO_LEAFREF;
Radek Krejcied5acc52019-04-25 15:57:04 +0200432 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800433#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200434 case 'P':
435 target_path = optarg;
436 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800437 case 's':
Radek Krejci4fa6ebf2019-11-21 13:34:35 +0800438 output_opts |= LYS_OUTPUT_NO_SUBSTMT;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800439 break;
440#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200441 case 'L':
442 tree_ll = atoi(optarg);
443 break;
444#endif
445 case '?':
446 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
447 goto cleanup;
448 }
449 }
450
451 /* file name */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800452 if (optind == argc && !target_path) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200453 fprintf(stderr, "Missing the module name.\n");
454 goto cleanup;
455 }
456
Radek Krejci77954e82019-04-30 13:51:29 +0200457#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200458 /* tree fromat with or without gropings */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800459 if ((output_opts || tree_ll) && format != LYS_OUT_TREE) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200460 fprintf(stderr, "--tree options take effect only in case of the tree output format.\n");
461 }
Radek Krejci77954e82019-04-30 13:51:29 +0200462#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200463
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800464 if (!target_path) {
465 /* module, revision */
466 model_name = argv[optind];
467 revision = NULL;
468 if (strchr(model_name, '@')) {
469 revision = strchr(model_name, '@');
470 revision[0] = '\0';
471 ++revision;
472 }
473
474 if (revision) {
475 module = ly_ctx_get_module(ctx, model_name, revision);
476 } else {
477 module = ly_ctx_get_module_latest(ctx, model_name);
478 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200479#if 0
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800480 if (!module) {
481 /* not a module, try to find it as a submodule */
482 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
483 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200484#endif
485
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800486 if (!module) {
487 if (revision) {
488 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
489 } else {
490 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
491 }
492 goto cleanup;
Radek Krejcied5acc52019-04-25 15:57:04 +0200493 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200494 }
495
496 if (out_path) {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200497 ret = ly_out_new_filepath(out_path, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100498 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200499 ret = ly_out_new_file(stdout, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100500 }
Radek Krejci84ce7b12020-06-11 17:28:25 +0200501 if (ret) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100502 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
503 goto cleanup;
Radek Krejcied5acc52019-04-25 15:57:04 +0200504 }
505
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800506 if (target_path) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100507 const struct lysc_node *node = lys_find_node(ctx, NULL, target_path);
508 if (node) {
509 ret = lys_print_node(out, node, format, tree_ll, output_opts);
510 } else {
511 fprintf(stderr, "The requested schema node \"%s\" does not exists.\n", target_path);
512 }
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800513 } else {
Radek Krejcia5bba312020-01-09 15:41:20 +0100514 ret = lys_print(out, module, format, tree_ll, output_opts);
Radek Krejcied5acc52019-04-25 15:57:04 +0200515 }
516
517cleanup:
518 free(*argv);
519 free(argv);
520
Radek Krejci241f6b52020-05-21 18:13:49 +0200521 ly_out_free(out, NULL, out_path ? 1 : 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200522
523 return ret;
524}
Radek Krejcie7b95092019-05-15 11:03:07 +0200525
Radek Krejcied5acc52019-04-25 15:57:04 +0200526static int
Michal Vaskof03ed032020-03-04 13:31:44 +0100527parse_data(char *filepath, int *options, const struct lyd_node *tree, const char *rpc_act_file,
Radek Krejcied5acc52019-04-25 15:57:04 +0200528 struct lyd_node **result)
529{
Radek Krejcied5acc52019-04-25 15:57:04 +0200530 struct lyd_node *data = NULL, *rpc_act = NULL;
531 int opts = *options;
Radek Krejci7931b192020-06-25 17:05:03 +0200532 struct ly_in *in;
Radek Krejcied5acc52019-04-25 15:57:04 +0200533
Radek Krejci7931b192020-06-25 17:05:03 +0200534 if (ly_in_new_filepath(filepath, 0, &in)) {
535 fprintf(stderr, "Unable to open input YANG data file \"%s\".", filepath);
Radek Krejcied5acc52019-04-25 15:57:04 +0200536 return EXIT_FAILURE;
537 }
538
Radek Krejcie7b95092019-05-15 11:03:07 +0200539#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200540 if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) {
541 /* automatically detect data type from the data top level */
542 if (informat != LYD_XML) {
543 fprintf(stderr, "Only XML data can be automatically explored.\n");
544 return EXIT_FAILURE;
545 }
546
547 xml = lyxml_parse_path(ctx, filepath, 0);
548 if (!xml) {
549 fprintf(stderr, "Failed to parse XML data for automatic type detection.\n");
550 return EXIT_FAILURE;
551 }
552
553 /* NOTE: namespace is ignored to simplify usage of this feature */
554
555 if (!strcmp(xml->name, "data")) {
556 fprintf(stdout, "Parsing %s as complete datastore.\n", filepath);
557 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_ADD_YANGLIB;
558 } else if (!strcmp(xml->name, "config")) {
559 fprintf(stdout, "Parsing %s as config data.\n", filepath);
560 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
561 } else if (!strcmp(xml->name, "get-reply")) {
562 fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath);
563 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
564 } else if (!strcmp(xml->name, "get-config-reply")) {
565 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath);
566 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
567 } else if (!strcmp(xml->name, "edit-config")) {
568 fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath);
569 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
570 } else if (!strcmp(xml->name, "rpc")) {
571 fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath);
572 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
573 } else if (!strcmp(xml->name, "rpc-reply")) {
574 if (!rpc_act_file) {
575 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
576 lyxml_free(ctx, xml);
577 return EXIT_FAILURE;
578 }
579 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath);
580 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
581 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
582 if (!rpc_act) {
583 fprintf(stderr, "Failed to parse RPC/action.\n");
584 lyxml_free(ctx, xml);
585 return EXIT_FAILURE;
586 }
587 } else if (!strcmp(xml->name, "notification")) {
588 fprintf(stdout, "Parsing %s as <notification> data.\n", filepath);
589 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
590 } else if (!strcmp(xml->name, "yang-data")) {
591 fprintf(stdout, "Parsing %s as <yang-data> data.\n", filepath);
592 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
593 if (!rpc_act_file) {
594 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
595 lyxml_free(ctx, xml);
596 return EXIT_FAILURE;
597 }
598 } else {
599 fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n");
600 lyxml_free(ctx, xml);
601 return EXIT_FAILURE;
602 }
603
604 if (opts & LYD_OPT_RPCREPLY) {
605 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act, val_tree);
606 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
607 data = lyd_parse_xml(ctx, &xml->child, opts, val_tree);
608 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
609 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act_file);
610 } else {
611 data = lyd_parse_xml(ctx, &xml->child, opts);
612 }
613 lyxml_free(ctx, xml);
614 } else {
615 if (opts & LYD_OPT_RPCREPLY) {
616 if (!rpc_act_file) {
617 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
618 return EXIT_FAILURE;
619 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200620 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200621 if (!rpc_act) {
622 fprintf(stderr, "Failed to parse RPC/action.\n");
623 return EXIT_FAILURE;
624 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200625 if (trees) {
626 const struct lyd_node **trees_new;
627 unsigned int u;
628 trees_new = lyd_trees_new(1, rpc_act);
629
630 LY_ARRAY_FOR(trees, u) {
631 trees_new = lyd_trees_add(trees_new, trees[u]);
632 }
633 lyd_trees_free(trees, 0);
634 trees = trees_new;
635 } else {
636 trees = lyd_trees_new(1, rpc_act);
637 }
638 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200639 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200640 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200641 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
642 if (!rpc_act_file) {
643 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
644 return EXIT_FAILURE;
645 }
646 data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file);
647 } else {
Michal Vaskoa3881362020-01-21 15:57:35 +0100648#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200649
650 lyd_parse_data(ctx, in, 0, opts, 0, &data);
Radek Krejcie7b95092019-05-15 11:03:07 +0200651#if 0
Michal Vaskoa3881362020-01-21 15:57:35 +0100652 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200653 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200654#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200655 ly_in_free(in, 0);
656
Radek Krejcie7b95092019-05-15 11:03:07 +0200657 lyd_free_all(rpc_act);
Radek Krejcied5acc52019-04-25 15:57:04 +0200658
Radek Krejcie7b95092019-05-15 11:03:07 +0200659 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200660 fprintf(stderr, "Failed to parse data.\n");
Radek Krejcie7b95092019-05-15 11:03:07 +0200661 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200662 return EXIT_FAILURE;
663 }
664
665 *result = data;
666 *options = opts;
667 return EXIT_SUCCESS;
668}
669
670int
671cmd_data(const char *arg)
672{
673 int c, argc, option_index, ret = 1;
674 int options = 0, printopt = 0;
675 char **argv = NULL, *ptr;
676 const char *out_path = NULL;
Michal Vaskof03ed032020-03-04 13:31:44 +0100677 struct lyd_node *data = NULL;
678 struct lyd_node *tree = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100679 LYD_FORMAT outformat = 0;
Radek Krejci241f6b52020-05-21 18:13:49 +0200680 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200681 static struct option long_options[] = {
682 {"defaults", required_argument, 0, 'd'},
683 {"help", no_argument, 0, 'h'},
684 {"format", required_argument, 0, 'f'},
685 {"option", required_argument, 0, 't'},
686 {"output", required_argument, 0, 'o'},
687 {"running", required_argument, 0, 'r'},
688 {"strict", no_argument, 0, 's'},
689 {NULL, 0, 0, 0}
690 };
691 void *rlcd;
692
693 argc = 1;
694 argv = malloc(2*sizeof *argv);
695 *argv = strdup(arg);
696 ptr = strtok(*argv, " ");
697 while ((ptr = strtok(NULL, " "))) {
698 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
699 if (!rlcd) {
700 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
701 goto cleanup;
702 }
703 argv = rlcd;
704 argv[argc++] = ptr;
705 }
706 argv[argc] = NULL;
707
708 optind = 0;
709 while (1) {
710 option_index = 0;
711 c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index);
712 if (c == -1) {
713 break;
714 }
715
716 switch (c) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200717#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200718 case 'd':
719 if (!strcmp(optarg, "all")) {
720 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
721 } else if (!strcmp(optarg, "all-tagged")) {
722 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
723 } else if (!strcmp(optarg, "trim")) {
724 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;
725 } else if (!strcmp(optarg, "implicit-tagged")) {
726 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
727 }
728 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200729#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200730 case 'h':
731 cmd_data_help();
732 ret = 0;
733 goto cleanup;
734 case 'f':
735 if (!strcmp(optarg, "xml")) {
736 outformat = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200737#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200738 } else if (!strcmp(optarg, "json")) {
739 outformat = LYD_JSON;
740 } else if (!strcmp(optarg, "lyb")) {
741 outformat = LYD_LYB;
Radek Krejcie7b95092019-05-15 11:03:07 +0200742#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200743 } else {
744 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
745 goto cleanup;
746 }
747 break;
748 case 'o':
749 if (out_path) {
750 fprintf(stderr, "Output specified twice.\n");
751 goto cleanup;
752 }
753 out_path = optarg;
754 break;
Michal Vaskoa3881362020-01-21 15:57:35 +0100755#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200756 case 'r':
Radek Krejcied5acc52019-04-25 15:57:04 +0200757 if (optarg[0] == '!') {
758 /* ignore extenral dependencies to the running datastore */
759 options |= LYD_OPT_NOEXTDEPS;
760 } else {
761 /* external file with the running datastore */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200762 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200763 if (!val_tree) {
764 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
765 goto cleanup;
766 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200767 if (!trees) {
768 trees = lyd_trees_new(1, val_tree);
769 } else {
770 trees = lyd_trees_add(trees, val_tree);
771 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200772 }
773 break;
774 case 's':
775 options |= LYD_OPT_STRICT;
776 options |= LYD_OPT_OBSOLETE;
777 break;
Michal Vaskoa3881362020-01-21 15:57:35 +0100778#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200779 case 't':
780 if (!strcmp(optarg, "auto")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100781 /* no flags */
Radek Krejcied5acc52019-04-25 15:57:04 +0200782 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100783 /* no flags */
Michal Vaskob36053d2020-03-26 15:49:30 +0100784 /*} else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100785 options |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200786 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100787 options |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200788 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100789 options |= LYD_OPT_GETCONFIG;
Michal Vaskob36053d2020-03-26 15:49:30 +0100790 } else if (!strcmp(optarg, "edit")) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100791 options |= LYD_OPT_EDIT;*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200792 } else {
793 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
794 cmd_data_help();
795 goto cleanup;
796 }
797 break;
798 case '?':
799 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
800 goto cleanup;
801 }
802 }
803
804 /* file name */
805 if (optind == argc) {
806 fprintf(stderr, "Missing the data file name.\n");
807 goto cleanup;
808 }
809
Michal Vaskof03ed032020-03-04 13:31:44 +0100810 if (parse_data(argv[optind], &options, tree, argv[optind + 1], &data)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200811 goto cleanup;
812 }
813
814 if (out_path) {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200815 ret = ly_out_new_filepath(out_path, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100816 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200817 ret = ly_out_new_file(stdout, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100818 }
Radek Krejci84ce7b12020-06-11 17:28:25 +0200819 if (ret) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100820 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
821 goto cleanup;
Radek Krejcied5acc52019-04-25 15:57:04 +0200822 }
823
Michal Vasko52927e22020-03-16 17:26:14 +0100824 if (outformat) {
Radek Krejci7931b192020-06-25 17:05:03 +0200825 ret = lyd_print(out, data, outformat, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_FORMAT | printopt);
Radek Krejci84ce7b12020-06-11 17:28:25 +0200826 ret = ret < 0 ? ret * (-1) : 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200827 }
828
Radek Krejcied5acc52019-04-25 15:57:04 +0200829cleanup:
830 free(*argv);
831 free(argv);
832
Radek Krejci241f6b52020-05-21 18:13:49 +0200833 ly_out_free(out, NULL, out_path ? 1 : 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200834
Radek Krejcie7b95092019-05-15 11:03:07 +0200835 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200836
837 return ret;
838}
Radek Krejcie7b95092019-05-15 11:03:07 +0200839#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200840int
841cmd_xpath(const char *arg)
842{
843 int c, argc, option_index, ret = 1, long_str;
844 char **argv = NULL, *ptr, *expr = NULL;
845 unsigned int i, j;
846 int options = 0;
847 struct lyd_node *data = NULL, *node, *val_tree = NULL;
848 struct lyd_node_leaf_list *key;
849 struct ly_set *set;
850 static struct option long_options[] = {
851 {"help", no_argument, 0, 'h'},
852 {"expr", required_argument, 0, 'e'},
853 {NULL, 0, 0, 0}
854 };
855 void *rlcd;
856
857 long_str = 0;
858 argc = 1;
859 argv = malloc(2 * sizeof *argv);
860 *argv = strdup(arg);
861 ptr = strtok(*argv, " ");
862 while ((ptr = strtok(NULL, " "))) {
863 if (long_str) {
864 ptr[-1] = ' ';
865 if (ptr[strlen(ptr) - 1] == long_str) {
866 long_str = 0;
867 ptr[strlen(ptr) - 1] = '\0';
868 }
869 } else {
870 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
871 if (!rlcd) {
872 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
873 goto cleanup;
874 }
875 argv = rlcd;
876 argv[argc] = ptr;
877 if (ptr[0] == '"') {
878 long_str = '"';
879 ++argv[argc];
880 }
881 if (ptr[0] == '\'') {
882 long_str = '\'';
883 ++argv[argc];
884 }
885 if (ptr[strlen(ptr) - 1] == long_str) {
886 long_str = 0;
887 ptr[strlen(ptr) - 1] = '\0';
888 }
889 ++argc;
890 }
891 }
892 argv[argc] = NULL;
893
894 optind = 0;
895 while (1) {
896 option_index = 0;
897 c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index);
898 if (c == -1) {
899 break;
900 }
901
902 switch (c) {
903 case 'h':
904 cmd_xpath_help();
905 ret = 0;
906 goto cleanup;
907 case 'e':
908 expr = optarg;
909 break;
910 case 't':
911 if (!strcmp(optarg, "auto")) {
912 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
913 } else if (!strcmp(optarg, "config")) {
914 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
915 } else if (!strcmp(optarg, "get")) {
916 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
917 } else if (!strcmp(optarg, "getconfig")) {
918 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
919 } else if (!strcmp(optarg, "edit")) {
920 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
921 } else if (!strcmp(optarg, "rpc")) {
922 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
923 } else if (!strcmp(optarg, "rpcreply")) {
924 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
925 } else if (!strcmp(optarg, "notif")) {
926 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
927 } else if (!strcmp(optarg, "yangdata")) {
928 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
929 } else {
930 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
931 cmd_data_help();
932 goto cleanup;
933 }
934 break;
935 case 'x':
936 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG);
937 if (!val_tree) {
938 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
939 goto cleanup;
940 }
941 break;
942 case '?':
943 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
944 goto cleanup;
945 }
946 }
947
948 if (optind == argc) {
949 fprintf(stderr, "Missing the file with data.\n");
950 goto cleanup;
951 }
952
953 if (!expr) {
954 fprintf(stderr, "Missing the XPath expression.\n");
955 goto cleanup;
956 }
957
958 if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
959 goto cleanup;
960 }
961
962 if (!(set = lyd_find_path(data, expr))) {
963 goto cleanup;
964 }
965
966 /* print result */
967 printf("Result:\n");
968 if (!set->number) {
969 printf("\tEmpty\n");
970 } else {
971 for (i = 0; i < set->number; ++i) {
972 node = set->set.d[i];
973 switch (node->schema->nodetype) {
974 case LYS_CONTAINER:
975 printf("\tContainer ");
976 break;
977 case LYS_LEAF:
978 printf("\tLeaf ");
979 break;
980 case LYS_LEAFLIST:
981 printf("\tLeaflist ");
982 break;
983 case LYS_LIST:
984 printf("\tList ");
985 break;
986 case LYS_ANYXML:
987 printf("\tAnyxml ");
988 break;
989 case LYS_ANYDATA:
990 printf("\tAnydata ");
991 break;
992 default:
993 printf("\tUnknown ");
994 break;
995 }
996 printf("\"%s\"", node->schema->name);
997 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
998 printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str);
999 } else if (node->schema->nodetype == LYS_LIST) {
1000 key = (struct lyd_node_leaf_list *)node->child;
1001 printf(" (");
1002 for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) {
1003 if (j) {
1004 printf(" ");
1005 }
1006 printf("\"%s\": %s", key->schema->name, key->value_str);
1007 key = (struct lyd_node_leaf_list *)key->next;
1008 }
1009 printf(")");
1010 }
1011 printf("\n");
1012 }
1013 }
1014 printf("\n");
1015
1016 ly_set_free(set);
1017 ret = 0;
1018
1019cleanup:
1020 free(*argv);
1021 free(argv);
1022
1023 lyd_free_withsiblings(data);
1024
1025 return ret;
1026}
1027
1028int
1029print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
1030{
1031 struct lyd_node *ylib;
1032 uint32_t idx = 0, has_modules = 0;
1033 uint8_t u;
1034 const struct lys_module *mod;
1035
1036 if (outformat != LYD_UNKNOWN) {
1037 ylib = ly_ctx_info(ctx);
1038 if (!ylib) {
1039 fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n");
1040 return 1;
1041 }
1042
1043 lyd_print_file(out, ylib, outformat, LYP_WITHSIBLINGS | LYP_FORMAT);
1044 lyd_free_withsiblings(ylib);
1045 return 0;
1046 }
1047
1048 /* iterate schemas in context and provide just the basic info */
1049 fprintf(out, "List of the loaded models:\n");
1050 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
1051 has_modules++;
1052
1053 /* conformance print */
1054 if (mod->implemented) {
1055 fprintf(out, "\tI");
1056 } else {
1057 fprintf(out, "\ti");
1058 }
1059
1060 /* module print */
1061 fprintf(out, " %s", mod->name);
1062 if (mod->rev_size) {
1063 fprintf(out, "@%s", mod->rev[0].date);
1064 }
1065
1066 /* submodules print */
1067 if (mod->inc_size) {
1068 fprintf(out, " (");
1069 for (u = 0; u < mod->inc_size; u++) {
1070 fprintf(out, "%s%s", !u ? "" : ",", mod->inc[u].submodule->name);
1071 if (mod->inc[u].submodule->rev_size) {
1072 fprintf(out, "@%s", mod->inc[u].submodule->rev[0].date);
1073 }
1074 }
1075 fprintf(out, ")");
1076 }
1077
1078 /* finish the line */
1079 fprintf(out, "\n");
1080 }
1081
1082 if (!has_modules) {
1083 fprintf(out, "\t(none)\n");
1084 }
1085
1086 return 0;
1087}
1088
1089int
1090cmd_list(const char *arg)
1091{
1092 char **argv = NULL, *ptr;
1093 int c, argc, option_index;
1094 LYD_FORMAT outformat = LYD_UNKNOWN;
1095 static struct option long_options[] = {
1096 {"help", no_argument, 0, 'h'},
1097 {"format", required_argument, 0, 'f'},
1098 {NULL, 0, 0, 0}
1099 };
1100 void *rlcd;
1101
1102 argc = 1;
1103 argv = malloc(2*sizeof *argv);
1104 *argv = strdup(arg);
1105 ptr = strtok(*argv, " ");
1106 while ((ptr = strtok(NULL, " "))) {
1107 rlcd = realloc(argv, (argc+2)*sizeof *argv);
1108 if (!rlcd) {
1109 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1110 goto error;
1111 }
1112 argv = rlcd;
1113 argv[argc++] = ptr;
1114 }
1115 argv[argc] = NULL;
1116
1117 optind = 0;
1118 while (1) {
1119 option_index = 0;
1120 c = getopt_long(argc, argv, "hf:", long_options, &option_index);
1121 if (c == -1) {
1122 break;
1123 }
1124
1125 switch (c) {
1126 case 'h':
1127 cmd_data_help();
1128 free(*argv);
1129 free(argv);
1130 return 0;
1131 case 'f':
1132 if (!strcmp(optarg, "xml")) {
1133 outformat = LYD_XML;
1134 } else if (!strcmp(optarg, "json")) {
1135 outformat = LYD_JSON;
1136 } else {
1137 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
1138 goto error;
1139 }
1140 break;
1141 case '?':
1142 /* getopt_long() prints message */
1143 goto error;
1144 }
1145 }
1146 if (optind != argc) {
1147 fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]);
1148error:
1149 free(*argv);
1150 free(argv);
1151 return 1;
1152 }
1153 free(*argv);
1154 free(argv);
1155
1156 return print_list(stdout, ctx, outformat);
1157}
1158#endif
1159int
1160cmd_feature(const char *arg)
1161{
1162 int c, argc, option_index, ret = 1, task = 0;
1163 char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL;
1164 const struct lys_module *module;
1165 static struct option long_options[] = {
1166 {"help", no_argument, 0, 'h'},
1167 {"enable", required_argument, 0, 'e'},
1168 {"disable", required_argument, 0, 'd'},
1169 {NULL, 0, 0, 0}
1170 };
1171 void *rlcd;
1172
1173 argc = 1;
1174 argv = malloc(2*sizeof *argv);
1175 *argv = strdup(arg);
1176 ptr = strtok(*argv, " ");
1177 while ((ptr = strtok(NULL, " "))) {
1178 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
1179 if (!rlcd) {
1180 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1181 goto cleanup;
1182 }
1183 argv = rlcd;
1184 argv[argc++] = ptr;
1185 }
1186 argv[argc] = NULL;
1187
1188 optind = 0;
1189 while (1) {
1190 option_index = 0;
1191 c = getopt_long(argc, argv, "he:d:", long_options, &option_index);
1192 if (c == -1) {
1193 break;
1194 }
1195
1196 switch (c) {
1197 case 'h':
1198 cmd_feature_help();
1199 ret = 0;
1200 goto cleanup;
1201 case 'e':
1202 if (task) {
1203 fprintf(stderr, "Only one of enable or disable can be specified.\n");
1204 goto cleanup;
1205 }
1206 task = 1;
1207 feat_names = optarg;
1208 break;
1209 case 'd':
1210 if (task) {
1211 fprintf(stderr, "Only one of enable, or disable can be specified.\n");
1212 goto cleanup;
1213 }
1214 task = 2;
1215 feat_names = optarg;
1216 break;
1217 case '?':
1218 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
1219 goto cleanup;
1220 }
1221 }
1222
1223 /* module name */
1224 if (optind == argc) {
1225 fprintf(stderr, "Missing the module name.\n");
1226 goto cleanup;
1227 }
1228
1229 revision = NULL;
1230 model_name = argv[optind];
1231 if (strchr(model_name, '@')) {
1232 revision = strchr(model_name, '@');
1233 revision[0] = '\0';
1234 ++revision;
1235 }
1236
1237 module = ly_ctx_get_module(ctx, model_name, revision);
1238#if 0
1239 if (!module) {
1240 /* not a module, try to find it as a submodule */
1241 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
1242 }
1243#endif
1244
1245 if (module == NULL) {
1246 if (revision) {
1247 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
1248 } else {
1249 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
1250 }
1251 goto cleanup;
1252 }
1253
1254 if (!task) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001255 size_t len, max_len = 0;
1256 LY_ARRAY_SIZE_TYPE u;
Radek Krejcied5acc52019-04-25 15:57:04 +02001257 struct lysc_feature *features;
1258
1259 printf("%s features:\n", module->name);
1260
1261 if (module->compiled) {
1262 features = module->compiled->features;
1263 } else {
1264 features = module->off_features;
1265 }
1266
1267 /* get the max len */
1268 LY_ARRAY_FOR(features, u) {
1269 len = strlen(features[u].name);
1270 if (len > max_len) {
1271 max_len = len;
1272 }
1273 }
1274
1275 LY_ARRAY_FOR(features, u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001276 printf("\t%-*s (%s)\n", (int)max_len, features[u].name, (features[u].flags & LYS_FENABLED) ? "on" : "off");
Radek Krejcied5acc52019-04-25 15:57:04 +02001277 }
1278 if (!u) {
1279 printf("\t(none)\n");
1280 }
1281 } else {
1282 feat_names = strtok(feat_names, ",");
1283 while (feat_names) {
1284 if (((task == 1) && lys_feature_enable(module, feat_names))
1285 || ((task == 2) && lys_feature_disable(module, feat_names))) {
1286 fprintf(stderr, "Feature \"%s\" not found.\n", feat_names);
1287 ret = 1;
1288 }
1289 feat_names = strtok(NULL, ",");
1290 }
1291 }
1292
1293cleanup:
1294 free(*argv);
1295 free(argv);
1296
1297 return ret;
1298}
1299
1300int
1301cmd_searchpath(const char *arg)
1302{
1303 const char *path;
1304 const char * const *searchpaths;
1305 int index;
1306 struct stat st;
1307
1308 for (path = strchr(arg, ' '); path && (path[0] == ' '); ++path);
1309 if (!path || (path[0] == '\0')) {
1310 searchpaths = ly_ctx_get_searchdirs(ctx);
1311 if (searchpaths) {
1312 for (index = 0; searchpaths[index]; index++) {
1313 fprintf(stdout, "%s\n", searchpaths[index]);
1314 }
1315 }
1316 return 0;
1317 }
1318
1319 if ((!strncmp(path, "-h", 2) && (path[2] == '\0' || path[2] == ' ')) ||
1320 (!strncmp(path, "--help", 6) && (path[6] == '\0' || path[6] == ' '))) {
1321 cmd_searchpath_help();
1322 return 0;
1323 } else if (!strncmp(path, "--clear", 7) && (path[7] == '\0' || path[7] == ' ')) {
1324 ly_ctx_unset_searchdirs(ctx, NULL);
1325 return 0;
1326 }
1327
1328 if (stat(path, &st) == -1) {
1329 fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno));
1330 return 1;
1331 }
1332 if (!S_ISDIR(st.st_mode)) {
1333 fprintf(stderr, "\"%s\" is not a directory.\n", path);
1334 return 1;
1335 }
1336
1337 ly_ctx_set_searchdir(ctx, path);
1338
1339 return 0;
1340}
1341
1342int
1343cmd_clear(const char *arg)
1344{
1345 struct ly_ctx *ctx_new;
1346 int options = 0;
1347#if 0
1348 int i;
1349 char *ylpath;
1350 const char * const *searchpaths;
1351 LYD_FORMAT format;
1352
1353 /* get optional yang library file name */
1354 for (i = 5; arg[i] && isspace(arg[i]); i++);
1355 if (arg[i]) {
1356 if (arg[i] == '-' && arg[i + 1] == 'e') {
1357 options = LY_CTX_NOYANGLIBRARY;
1358 goto create_empty;
1359 } else {
1360 ylpath = strdup(&arg[i]);
1361 format = detect_data_format(ylpath);
1362 if (format == LYD_UNKNOWN) {
1363 free(ylpath);
1364 fprintf(stderr, "Unable to resolve format of the yang library file, please add \".xml\" or \".json\" suffix.\n");
1365 goto create_empty;
1366 }
1367 searchpaths = ly_ctx_get_searchdirs(ctx);
1368 ctx_new = ly_ctx_new_ylpath(searchpaths ? searchpaths[0] : NULL, ylpath, format, 0);
1369 free(ylpath);
1370 }
1371 } else {
1372create_empty:
1373#else
Radek Krejci92f5ce92019-09-06 16:25:43 +02001374 (void) arg; /* TODO yang-library support */
Radek Krejcied5acc52019-04-25 15:57:04 +02001375 {
1376#endif
1377 ly_ctx_new(NULL, options, &ctx_new);
1378 }
1379
1380 if (!ctx_new) {
1381 fprintf(stderr, "Failed to create context.\n");
1382 return 1;
1383 }
1384
1385 /* final switch */
1386 ly_ctx_destroy(ctx, NULL);
1387 ctx = ctx_new;
1388
1389 return 0;
1390}
1391
1392int
1393cmd_verb(const char *arg)
1394{
1395 const char *verb;
1396 if (strlen(arg) < 5) {
1397 cmd_verb_help();
1398 return 1;
1399 }
1400
1401 verb = arg + 5;
1402 if (!strcmp(verb, "error") || !strcmp(verb, "0")) {
1403 ly_verb(LY_LLERR);
1404#ifndef NDEBUG
1405 ly_verb_dbg(0);
1406#endif
1407 } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) {
1408 ly_verb(LY_LLWRN);
1409#ifndef NDEBUG
1410 ly_verb_dbg(0);
1411#endif
1412 } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) {
1413 ly_verb(LY_LLVRB);
1414#ifndef NDEBUG
1415 ly_verb_dbg(0);
1416#endif
1417 } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) {
1418 ly_verb(LY_LLDBG);
1419#ifndef NDEBUG
1420 ly_verb_dbg(LY_LDGDICT | LY_LDGYANG | LY_LDGYIN | LY_LDGXPATH | LY_LDGDIFF);
1421#endif
1422 } else {
1423 fprintf(stderr, "Unknown verbosity \"%s\"\n", verb);
1424 return 1;
1425 }
1426
1427 return 0;
1428}
1429
1430#ifndef NDEBUG
1431
1432int
1433cmd_debug(const char *arg)
1434{
1435 const char *beg, *end;
1436 int grps = 0;
1437 if (strlen(arg) < 6) {
1438 cmd_debug_help();
1439 return 1;
1440 }
1441
1442 end = arg + 6;
1443 while (end[0]) {
1444 for (beg = end; isspace(beg[0]); ++beg);
1445 if (!beg[0]) {
1446 break;
1447 }
1448
1449 for (end = beg; (end[0] && !isspace(end[0])); ++end);
1450
1451 if (!strncmp(beg, "dict", end - beg)) {
1452 grps |= LY_LDGDICT;
1453 } else if (!strncmp(beg, "yang", end - beg)) {
1454 grps |= LY_LDGYANG;
1455 } else if (!strncmp(beg, "yin", end - beg)) {
1456 grps |= LY_LDGYIN;
1457 } else if (!strncmp(beg, "xpath", end - beg)) {
1458 grps |= LY_LDGXPATH;
1459 } else if (!strncmp(beg, "diff", end - beg)) {
1460 grps |= LY_LDGDIFF;
1461 } else {
1462 fprintf(stderr, "Unknown debug group \"%.*s\"\n", (int)(end - beg), beg);
1463 return 1;
1464 }
1465 }
1466 ly_verb_dbg(grps);
1467
1468 return 0;
1469}
1470
1471#endif
1472
1473int
1474cmd_quit(const char *UNUSED(arg))
1475{
1476 done = 1;
1477 return 0;
1478}
1479
1480int
1481cmd_help(const char *arg)
1482{
1483 int i;
1484 char *args = strdup(arg);
1485 char *cmd = NULL;
1486
1487 strtok(args, " ");
1488 if ((cmd = strtok(NULL, " ")) == NULL) {
1489
1490generic_help:
1491 fprintf(stdout, "Available commands:\n");
1492
1493 for (i = 0; commands[i].name; i++) {
1494 if (commands[i].helpstring != NULL) {
1495 fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring);
1496 }
1497 }
1498 } else {
1499 /* print specific help for the selected command */
1500
1501 /* get the command of the specified name */
1502 for (i = 0; commands[i].name; i++) {
1503 if (strcmp(cmd, commands[i].name) == 0) {
1504 break;
1505 }
1506 }
1507
1508 /* execute the command's help if any valid command specified */
1509 if (commands[i].name) {
1510 if (commands[i].help_func != NULL) {
1511 commands[i].help_func();
1512 } else {
1513 printf("%s\n", commands[i].helpstring);
1514 }
1515 } else {
1516 /* if unknown command specified, print the list of commands */
1517 printf("Unknown command \'%s\'\n", cmd);
1518 goto generic_help;
1519 }
1520 }
1521
1522 free(args);
1523 return 0;
1524}
1525
1526COMMAND commands[] = {
1527 {"help", cmd_help, NULL, "Display commands description"},
1528 {"add", cmd_add, cmd_add_help, "Add a new model from a specific file"},
1529 {"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
1530 {"print", cmd_print, cmd_print_help, "Print a model"},
Radek Krejcied5acc52019-04-25 15:57:04 +02001531 {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
Radek Krejcie7b95092019-05-15 11:03:07 +02001532#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001533 {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"},
1534 {"list", cmd_list, cmd_list_help, "List all the loaded models"},
1535#endif
1536 {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"},
1537 {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"},
1538 {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"},
1539 {"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
1540#ifndef NDEBUG
1541 {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
1542#endif
1543 {"quit", cmd_quit, NULL, "Quit the program"},
1544 /* synonyms for previous commands */
1545 {"?", cmd_help, NULL, "Display commands description"},
1546 {"exit", cmd_quit, NULL, "Quit the program"},
1547 {NULL, NULL, NULL, NULL}
1548};