blob: 42f1544edfc6acbe786cc202863a9b40a6adbcb0 [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{
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;
349 FILE *output = stdout;
350 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) {
497 output = fopen(out_path, "w");
498 if (!output) {
499 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
500 goto cleanup;
501 }
502 }
503
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800504 if (target_path) {
505 ret = lys_node_print_file(output, ctx, NULL, format, target_path, tree_ll, output_opts);
506 } else {
507 ret = lys_print_file(output, module, format, tree_ll, output_opts);
Radek Krejcied5acc52019-04-25 15:57:04 +0200508 }
509
510cleanup:
511 free(*argv);
512 free(argv);
513
514 if (output && (output != stdout)) {
515 fclose(output);
516 }
517
518 return ret;
519}
Radek Krejcie7b95092019-05-15 11:03:07 +0200520
Radek Krejcied5acc52019-04-25 15:57:04 +0200521static LYD_FORMAT
522detect_data_format(char *filepath)
523{
524 size_t len;
525
526 /* detect input format according to file suffix */
527 len = strlen(filepath);
528 for (; isspace(filepath[len - 1]); len--, filepath[len] = '\0'); /* remove trailing whitespaces */
529 if (len >= 5 && !strcmp(&filepath[len - 4], ".xml")) {
530 return LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200531#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200532 } else if (len >= 6 && !strcmp(&filepath[len - 5], ".json")) {
533 return LYD_JSON;
534 } else if (len >= 5 && !strcmp(&filepath[len - 4], ".lyb")) {
535 return LYD_LYB;
Radek Krejcie7b95092019-05-15 11:03:07 +0200536#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200537 } else {
Michal Vasko52927e22020-03-16 17:26:14 +0100538 return 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200539 }
540}
541
542static int
Michal Vaskof03ed032020-03-04 13:31:44 +0100543parse_data(char *filepath, int *options, const struct lyd_node *tree, const char *rpc_act_file,
Radek Krejcied5acc52019-04-25 15:57:04 +0200544 struct lyd_node **result)
545{
Michal Vasko52927e22020-03-16 17:26:14 +0100546 LYD_FORMAT informat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200547 struct lyd_node *data = NULL, *rpc_act = NULL;
548 int opts = *options;
549
550 /* detect input format according to file suffix */
551 informat = detect_data_format(filepath);
Michal Vasko52927e22020-03-16 17:26:14 +0100552 if (!informat) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200553 fprintf(stderr, "Unable to resolve format of the input file, please add \".xml\", \".json\", or \".lyb\" suffix.\n");
554 return EXIT_FAILURE;
555 }
556
Radek Krejcie7b95092019-05-15 11:03:07 +0200557 ly_err_clean(ctx, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200558
Radek Krejcie7b95092019-05-15 11:03:07 +0200559#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200560 if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) {
561 /* automatically detect data type from the data top level */
562 if (informat != LYD_XML) {
563 fprintf(stderr, "Only XML data can be automatically explored.\n");
564 return EXIT_FAILURE;
565 }
566
567 xml = lyxml_parse_path(ctx, filepath, 0);
568 if (!xml) {
569 fprintf(stderr, "Failed to parse XML data for automatic type detection.\n");
570 return EXIT_FAILURE;
571 }
572
573 /* NOTE: namespace is ignored to simplify usage of this feature */
574
575 if (!strcmp(xml->name, "data")) {
576 fprintf(stdout, "Parsing %s as complete datastore.\n", filepath);
577 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_ADD_YANGLIB;
578 } else if (!strcmp(xml->name, "config")) {
579 fprintf(stdout, "Parsing %s as config data.\n", filepath);
580 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
581 } else if (!strcmp(xml->name, "get-reply")) {
582 fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath);
583 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
584 } else if (!strcmp(xml->name, "get-config-reply")) {
585 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath);
586 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
587 } else if (!strcmp(xml->name, "edit-config")) {
588 fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath);
589 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
590 } else if (!strcmp(xml->name, "rpc")) {
591 fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath);
592 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
593 } else if (!strcmp(xml->name, "rpc-reply")) {
594 if (!rpc_act_file) {
595 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
596 lyxml_free(ctx, xml);
597 return EXIT_FAILURE;
598 }
599 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath);
600 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
601 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
602 if (!rpc_act) {
603 fprintf(stderr, "Failed to parse RPC/action.\n");
604 lyxml_free(ctx, xml);
605 return EXIT_FAILURE;
606 }
607 } else if (!strcmp(xml->name, "notification")) {
608 fprintf(stdout, "Parsing %s as <notification> data.\n", filepath);
609 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
610 } else if (!strcmp(xml->name, "yang-data")) {
611 fprintf(stdout, "Parsing %s as <yang-data> data.\n", filepath);
612 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
613 if (!rpc_act_file) {
614 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
615 lyxml_free(ctx, xml);
616 return EXIT_FAILURE;
617 }
618 } else {
619 fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n");
620 lyxml_free(ctx, xml);
621 return EXIT_FAILURE;
622 }
623
624 if (opts & LYD_OPT_RPCREPLY) {
625 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act, val_tree);
626 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
627 data = lyd_parse_xml(ctx, &xml->child, opts, val_tree);
628 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
629 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act_file);
630 } else {
631 data = lyd_parse_xml(ctx, &xml->child, opts);
632 }
633 lyxml_free(ctx, xml);
634 } else {
635 if (opts & LYD_OPT_RPCREPLY) {
636 if (!rpc_act_file) {
637 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
638 return EXIT_FAILURE;
639 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200640 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200641 if (!rpc_act) {
642 fprintf(stderr, "Failed to parse RPC/action.\n");
643 return EXIT_FAILURE;
644 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200645 if (trees) {
646 const struct lyd_node **trees_new;
647 unsigned int u;
648 trees_new = lyd_trees_new(1, rpc_act);
649
650 LY_ARRAY_FOR(trees, u) {
651 trees_new = lyd_trees_add(trees_new, trees[u]);
652 }
653 lyd_trees_free(trees, 0);
654 trees = trees_new;
655 } else {
656 trees = lyd_trees_new(1, rpc_act);
657 }
658 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200659 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200660 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200661 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
662 if (!rpc_act_file) {
663 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
664 return EXIT_FAILURE;
665 }
666 data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file);
667 } else {
Michal Vaskoa3881362020-01-21 15:57:35 +0100668#endif
669 data = lyd_parse_path(ctx, filepath, informat, opts);
Radek Krejcie7b95092019-05-15 11:03:07 +0200670#if 0
Michal Vaskoa3881362020-01-21 15:57:35 +0100671 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200672 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200673#endif
674 lyd_free_all(rpc_act);
Radek Krejcied5acc52019-04-25 15:57:04 +0200675
Radek Krejcie7b95092019-05-15 11:03:07 +0200676 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200677 fprintf(stderr, "Failed to parse data.\n");
Radek Krejcie7b95092019-05-15 11:03:07 +0200678 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200679 return EXIT_FAILURE;
680 }
681
682 *result = data;
683 *options = opts;
684 return EXIT_SUCCESS;
685}
686
687int
688cmd_data(const char *arg)
689{
690 int c, argc, option_index, ret = 1;
691 int options = 0, printopt = 0;
692 char **argv = NULL, *ptr;
693 const char *out_path = NULL;
Michal Vaskof03ed032020-03-04 13:31:44 +0100694 struct lyd_node *data = NULL;
695 struct lyd_node *tree = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100696 LYD_FORMAT outformat = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200697 FILE *output = stdout;
698 static struct option long_options[] = {
699 {"defaults", required_argument, 0, 'd'},
700 {"help", no_argument, 0, 'h'},
701 {"format", required_argument, 0, 'f'},
702 {"option", required_argument, 0, 't'},
703 {"output", required_argument, 0, 'o'},
704 {"running", required_argument, 0, 'r'},
705 {"strict", no_argument, 0, 's'},
706 {NULL, 0, 0, 0}
707 };
708 void *rlcd;
709
710 argc = 1;
711 argv = malloc(2*sizeof *argv);
712 *argv = strdup(arg);
713 ptr = strtok(*argv, " ");
714 while ((ptr = strtok(NULL, " "))) {
715 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
716 if (!rlcd) {
717 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
718 goto cleanup;
719 }
720 argv = rlcd;
721 argv[argc++] = ptr;
722 }
723 argv[argc] = NULL;
724
725 optind = 0;
726 while (1) {
727 option_index = 0;
728 c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index);
729 if (c == -1) {
730 break;
731 }
732
733 switch (c) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200734#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200735 case 'd':
736 if (!strcmp(optarg, "all")) {
737 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
738 } else if (!strcmp(optarg, "all-tagged")) {
739 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
740 } else if (!strcmp(optarg, "trim")) {
741 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;
742 } else if (!strcmp(optarg, "implicit-tagged")) {
743 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
744 }
745 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200746#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200747 case 'h':
748 cmd_data_help();
749 ret = 0;
750 goto cleanup;
751 case 'f':
752 if (!strcmp(optarg, "xml")) {
753 outformat = LYD_XML;
Radek Krejcie7b95092019-05-15 11:03:07 +0200754#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200755 } else if (!strcmp(optarg, "json")) {
756 outformat = LYD_JSON;
757 } else if (!strcmp(optarg, "lyb")) {
758 outformat = LYD_LYB;
Radek Krejcie7b95092019-05-15 11:03:07 +0200759#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200760 } else {
761 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
762 goto cleanup;
763 }
764 break;
765 case 'o':
766 if (out_path) {
767 fprintf(stderr, "Output specified twice.\n");
768 goto cleanup;
769 }
770 out_path = optarg;
771 break;
Michal Vaskoa3881362020-01-21 15:57:35 +0100772#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200773 case 'r':
Radek Krejcied5acc52019-04-25 15:57:04 +0200774 if (optarg[0] == '!') {
775 /* ignore extenral dependencies to the running datastore */
776 options |= LYD_OPT_NOEXTDEPS;
777 } else {
778 /* external file with the running datastore */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200779 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200780 if (!val_tree) {
781 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
782 goto cleanup;
783 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200784 if (!trees) {
785 trees = lyd_trees_new(1, val_tree);
786 } else {
787 trees = lyd_trees_add(trees, val_tree);
788 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200789 }
790 break;
791 case 's':
792 options |= LYD_OPT_STRICT;
793 options |= LYD_OPT_OBSOLETE;
794 break;
Michal Vaskoa3881362020-01-21 15:57:35 +0100795#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200796 case 't':
797 if (!strcmp(optarg, "auto")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100798 /* no flags */
Radek Krejcied5acc52019-04-25 15:57:04 +0200799 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100800 /* no flags */
Michal Vaskob36053d2020-03-26 15:49:30 +0100801 /*} else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100802 options |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200803 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100804 options |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200805 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100806 options |= LYD_OPT_GETCONFIG;
Michal Vaskob36053d2020-03-26 15:49:30 +0100807 } else if (!strcmp(optarg, "edit")) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100808 options |= LYD_OPT_EDIT;*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200809 } else {
810 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
811 cmd_data_help();
812 goto cleanup;
813 }
814 break;
815 case '?':
816 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
817 goto cleanup;
818 }
819 }
820
821 /* file name */
822 if (optind == argc) {
823 fprintf(stderr, "Missing the data file name.\n");
824 goto cleanup;
825 }
826
Michal Vaskof03ed032020-03-04 13:31:44 +0100827 if (parse_data(argv[optind], &options, tree, argv[optind + 1], &data)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200828 goto cleanup;
829 }
830
831 if (out_path) {
832 output = fopen(out_path, "w");
833 if (!output) {
834 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
835 goto cleanup;
836 }
837 }
838
Michal Vasko52927e22020-03-16 17:26:14 +0100839 if (outformat) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100840 lyd_print_file(output, data, outformat, LYDP_WITHSIBLINGS | LYDP_FORMAT | printopt);
Radek Krejcied5acc52019-04-25 15:57:04 +0200841 }
842
843 ret = 0;
844
845cleanup:
846 free(*argv);
847 free(argv);
848
849 if (output && (output != stdout)) {
850 fclose(output);
851 }
852
Radek Krejcie7b95092019-05-15 11:03:07 +0200853 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200854
855 return ret;
856}
Radek Krejcie7b95092019-05-15 11:03:07 +0200857#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200858int
859cmd_xpath(const char *arg)
860{
861 int c, argc, option_index, ret = 1, long_str;
862 char **argv = NULL, *ptr, *expr = NULL;
863 unsigned int i, j;
864 int options = 0;
865 struct lyd_node *data = NULL, *node, *val_tree = NULL;
866 struct lyd_node_leaf_list *key;
867 struct ly_set *set;
868 static struct option long_options[] = {
869 {"help", no_argument, 0, 'h'},
870 {"expr", required_argument, 0, 'e'},
871 {NULL, 0, 0, 0}
872 };
873 void *rlcd;
874
875 long_str = 0;
876 argc = 1;
877 argv = malloc(2 * sizeof *argv);
878 *argv = strdup(arg);
879 ptr = strtok(*argv, " ");
880 while ((ptr = strtok(NULL, " "))) {
881 if (long_str) {
882 ptr[-1] = ' ';
883 if (ptr[strlen(ptr) - 1] == long_str) {
884 long_str = 0;
885 ptr[strlen(ptr) - 1] = '\0';
886 }
887 } else {
888 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
889 if (!rlcd) {
890 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
891 goto cleanup;
892 }
893 argv = rlcd;
894 argv[argc] = ptr;
895 if (ptr[0] == '"') {
896 long_str = '"';
897 ++argv[argc];
898 }
899 if (ptr[0] == '\'') {
900 long_str = '\'';
901 ++argv[argc];
902 }
903 if (ptr[strlen(ptr) - 1] == long_str) {
904 long_str = 0;
905 ptr[strlen(ptr) - 1] = '\0';
906 }
907 ++argc;
908 }
909 }
910 argv[argc] = NULL;
911
912 optind = 0;
913 while (1) {
914 option_index = 0;
915 c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index);
916 if (c == -1) {
917 break;
918 }
919
920 switch (c) {
921 case 'h':
922 cmd_xpath_help();
923 ret = 0;
924 goto cleanup;
925 case 'e':
926 expr = optarg;
927 break;
928 case 't':
929 if (!strcmp(optarg, "auto")) {
930 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
931 } else if (!strcmp(optarg, "config")) {
932 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
933 } else if (!strcmp(optarg, "get")) {
934 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
935 } else if (!strcmp(optarg, "getconfig")) {
936 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
937 } else if (!strcmp(optarg, "edit")) {
938 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
939 } else if (!strcmp(optarg, "rpc")) {
940 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
941 } else if (!strcmp(optarg, "rpcreply")) {
942 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
943 } else if (!strcmp(optarg, "notif")) {
944 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
945 } else if (!strcmp(optarg, "yangdata")) {
946 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
947 } else {
948 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
949 cmd_data_help();
950 goto cleanup;
951 }
952 break;
953 case 'x':
954 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG);
955 if (!val_tree) {
956 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
957 goto cleanup;
958 }
959 break;
960 case '?':
961 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
962 goto cleanup;
963 }
964 }
965
966 if (optind == argc) {
967 fprintf(stderr, "Missing the file with data.\n");
968 goto cleanup;
969 }
970
971 if (!expr) {
972 fprintf(stderr, "Missing the XPath expression.\n");
973 goto cleanup;
974 }
975
976 if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
977 goto cleanup;
978 }
979
980 if (!(set = lyd_find_path(data, expr))) {
981 goto cleanup;
982 }
983
984 /* print result */
985 printf("Result:\n");
986 if (!set->number) {
987 printf("\tEmpty\n");
988 } else {
989 for (i = 0; i < set->number; ++i) {
990 node = set->set.d[i];
991 switch (node->schema->nodetype) {
992 case LYS_CONTAINER:
993 printf("\tContainer ");
994 break;
995 case LYS_LEAF:
996 printf("\tLeaf ");
997 break;
998 case LYS_LEAFLIST:
999 printf("\tLeaflist ");
1000 break;
1001 case LYS_LIST:
1002 printf("\tList ");
1003 break;
1004 case LYS_ANYXML:
1005 printf("\tAnyxml ");
1006 break;
1007 case LYS_ANYDATA:
1008 printf("\tAnydata ");
1009 break;
1010 default:
1011 printf("\tUnknown ");
1012 break;
1013 }
1014 printf("\"%s\"", node->schema->name);
1015 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1016 printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str);
1017 } else if (node->schema->nodetype == LYS_LIST) {
1018 key = (struct lyd_node_leaf_list *)node->child;
1019 printf(" (");
1020 for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) {
1021 if (j) {
1022 printf(" ");
1023 }
1024 printf("\"%s\": %s", key->schema->name, key->value_str);
1025 key = (struct lyd_node_leaf_list *)key->next;
1026 }
1027 printf(")");
1028 }
1029 printf("\n");
1030 }
1031 }
1032 printf("\n");
1033
1034 ly_set_free(set);
1035 ret = 0;
1036
1037cleanup:
1038 free(*argv);
1039 free(argv);
1040
1041 lyd_free_withsiblings(data);
1042
1043 return ret;
1044}
1045
1046int
1047print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
1048{
1049 struct lyd_node *ylib;
1050 uint32_t idx = 0, has_modules = 0;
1051 uint8_t u;
1052 const struct lys_module *mod;
1053
1054 if (outformat != LYD_UNKNOWN) {
1055 ylib = ly_ctx_info(ctx);
1056 if (!ylib) {
1057 fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n");
1058 return 1;
1059 }
1060
1061 lyd_print_file(out, ylib, outformat, LYP_WITHSIBLINGS | LYP_FORMAT);
1062 lyd_free_withsiblings(ylib);
1063 return 0;
1064 }
1065
1066 /* iterate schemas in context and provide just the basic info */
1067 fprintf(out, "List of the loaded models:\n");
1068 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
1069 has_modules++;
1070
1071 /* conformance print */
1072 if (mod->implemented) {
1073 fprintf(out, "\tI");
1074 } else {
1075 fprintf(out, "\ti");
1076 }
1077
1078 /* module print */
1079 fprintf(out, " %s", mod->name);
1080 if (mod->rev_size) {
1081 fprintf(out, "@%s", mod->rev[0].date);
1082 }
1083
1084 /* submodules print */
1085 if (mod->inc_size) {
1086 fprintf(out, " (");
1087 for (u = 0; u < mod->inc_size; u++) {
1088 fprintf(out, "%s%s", !u ? "" : ",", mod->inc[u].submodule->name);
1089 if (mod->inc[u].submodule->rev_size) {
1090 fprintf(out, "@%s", mod->inc[u].submodule->rev[0].date);
1091 }
1092 }
1093 fprintf(out, ")");
1094 }
1095
1096 /* finish the line */
1097 fprintf(out, "\n");
1098 }
1099
1100 if (!has_modules) {
1101 fprintf(out, "\t(none)\n");
1102 }
1103
1104 return 0;
1105}
1106
1107int
1108cmd_list(const char *arg)
1109{
1110 char **argv = NULL, *ptr;
1111 int c, argc, option_index;
1112 LYD_FORMAT outformat = LYD_UNKNOWN;
1113 static struct option long_options[] = {
1114 {"help", no_argument, 0, 'h'},
1115 {"format", required_argument, 0, 'f'},
1116 {NULL, 0, 0, 0}
1117 };
1118 void *rlcd;
1119
1120 argc = 1;
1121 argv = malloc(2*sizeof *argv);
1122 *argv = strdup(arg);
1123 ptr = strtok(*argv, " ");
1124 while ((ptr = strtok(NULL, " "))) {
1125 rlcd = realloc(argv, (argc+2)*sizeof *argv);
1126 if (!rlcd) {
1127 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1128 goto error;
1129 }
1130 argv = rlcd;
1131 argv[argc++] = ptr;
1132 }
1133 argv[argc] = NULL;
1134
1135 optind = 0;
1136 while (1) {
1137 option_index = 0;
1138 c = getopt_long(argc, argv, "hf:", long_options, &option_index);
1139 if (c == -1) {
1140 break;
1141 }
1142
1143 switch (c) {
1144 case 'h':
1145 cmd_data_help();
1146 free(*argv);
1147 free(argv);
1148 return 0;
1149 case 'f':
1150 if (!strcmp(optarg, "xml")) {
1151 outformat = LYD_XML;
1152 } else if (!strcmp(optarg, "json")) {
1153 outformat = LYD_JSON;
1154 } else {
1155 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
1156 goto error;
1157 }
1158 break;
1159 case '?':
1160 /* getopt_long() prints message */
1161 goto error;
1162 }
1163 }
1164 if (optind != argc) {
1165 fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]);
1166error:
1167 free(*argv);
1168 free(argv);
1169 return 1;
1170 }
1171 free(*argv);
1172 free(argv);
1173
1174 return print_list(stdout, ctx, outformat);
1175}
1176#endif
1177int
1178cmd_feature(const char *arg)
1179{
1180 int c, argc, option_index, ret = 1, task = 0;
1181 char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL;
1182 const struct lys_module *module;
1183 static struct option long_options[] = {
1184 {"help", no_argument, 0, 'h'},
1185 {"enable", required_argument, 0, 'e'},
1186 {"disable", required_argument, 0, 'd'},
1187 {NULL, 0, 0, 0}
1188 };
1189 void *rlcd;
1190
1191 argc = 1;
1192 argv = malloc(2*sizeof *argv);
1193 *argv = strdup(arg);
1194 ptr = strtok(*argv, " ");
1195 while ((ptr = strtok(NULL, " "))) {
1196 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
1197 if (!rlcd) {
1198 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1199 goto cleanup;
1200 }
1201 argv = rlcd;
1202 argv[argc++] = ptr;
1203 }
1204 argv[argc] = NULL;
1205
1206 optind = 0;
1207 while (1) {
1208 option_index = 0;
1209 c = getopt_long(argc, argv, "he:d:", long_options, &option_index);
1210 if (c == -1) {
1211 break;
1212 }
1213
1214 switch (c) {
1215 case 'h':
1216 cmd_feature_help();
1217 ret = 0;
1218 goto cleanup;
1219 case 'e':
1220 if (task) {
1221 fprintf(stderr, "Only one of enable or disable can be specified.\n");
1222 goto cleanup;
1223 }
1224 task = 1;
1225 feat_names = optarg;
1226 break;
1227 case 'd':
1228 if (task) {
1229 fprintf(stderr, "Only one of enable, or disable can be specified.\n");
1230 goto cleanup;
1231 }
1232 task = 2;
1233 feat_names = optarg;
1234 break;
1235 case '?':
1236 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
1237 goto cleanup;
1238 }
1239 }
1240
1241 /* module name */
1242 if (optind == argc) {
1243 fprintf(stderr, "Missing the module name.\n");
1244 goto cleanup;
1245 }
1246
1247 revision = NULL;
1248 model_name = argv[optind];
1249 if (strchr(model_name, '@')) {
1250 revision = strchr(model_name, '@');
1251 revision[0] = '\0';
1252 ++revision;
1253 }
1254
1255 module = ly_ctx_get_module(ctx, model_name, revision);
1256#if 0
1257 if (!module) {
1258 /* not a module, try to find it as a submodule */
1259 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
1260 }
1261#endif
1262
1263 if (module == NULL) {
1264 if (revision) {
1265 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
1266 } else {
1267 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
1268 }
1269 goto cleanup;
1270 }
1271
1272 if (!task) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001273 size_t len, max_len = 0;
1274 LY_ARRAY_SIZE_TYPE u;
Radek Krejcied5acc52019-04-25 15:57:04 +02001275 struct lysc_feature *features;
1276
1277 printf("%s features:\n", module->name);
1278
1279 if (module->compiled) {
1280 features = module->compiled->features;
1281 } else {
1282 features = module->off_features;
1283 }
1284
1285 /* get the max len */
1286 LY_ARRAY_FOR(features, u) {
1287 len = strlen(features[u].name);
1288 if (len > max_len) {
1289 max_len = len;
1290 }
1291 }
1292
1293 LY_ARRAY_FOR(features, u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001294 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 +02001295 }
1296 if (!u) {
1297 printf("\t(none)\n");
1298 }
1299 } else {
1300 feat_names = strtok(feat_names, ",");
1301 while (feat_names) {
1302 if (((task == 1) && lys_feature_enable(module, feat_names))
1303 || ((task == 2) && lys_feature_disable(module, feat_names))) {
1304 fprintf(stderr, "Feature \"%s\" not found.\n", feat_names);
1305 ret = 1;
1306 }
1307 feat_names = strtok(NULL, ",");
1308 }
1309 }
1310
1311cleanup:
1312 free(*argv);
1313 free(argv);
1314
1315 return ret;
1316}
1317
1318int
1319cmd_searchpath(const char *arg)
1320{
1321 const char *path;
1322 const char * const *searchpaths;
1323 int index;
1324 struct stat st;
1325
1326 for (path = strchr(arg, ' '); path && (path[0] == ' '); ++path);
1327 if (!path || (path[0] == '\0')) {
1328 searchpaths = ly_ctx_get_searchdirs(ctx);
1329 if (searchpaths) {
1330 for (index = 0; searchpaths[index]; index++) {
1331 fprintf(stdout, "%s\n", searchpaths[index]);
1332 }
1333 }
1334 return 0;
1335 }
1336
1337 if ((!strncmp(path, "-h", 2) && (path[2] == '\0' || path[2] == ' ')) ||
1338 (!strncmp(path, "--help", 6) && (path[6] == '\0' || path[6] == ' '))) {
1339 cmd_searchpath_help();
1340 return 0;
1341 } else if (!strncmp(path, "--clear", 7) && (path[7] == '\0' || path[7] == ' ')) {
1342 ly_ctx_unset_searchdirs(ctx, NULL);
1343 return 0;
1344 }
1345
1346 if (stat(path, &st) == -1) {
1347 fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno));
1348 return 1;
1349 }
1350 if (!S_ISDIR(st.st_mode)) {
1351 fprintf(stderr, "\"%s\" is not a directory.\n", path);
1352 return 1;
1353 }
1354
1355 ly_ctx_set_searchdir(ctx, path);
1356
1357 return 0;
1358}
1359
1360int
1361cmd_clear(const char *arg)
1362{
1363 struct ly_ctx *ctx_new;
1364 int options = 0;
1365#if 0
1366 int i;
1367 char *ylpath;
1368 const char * const *searchpaths;
1369 LYD_FORMAT format;
1370
1371 /* get optional yang library file name */
1372 for (i = 5; arg[i] && isspace(arg[i]); i++);
1373 if (arg[i]) {
1374 if (arg[i] == '-' && arg[i + 1] == 'e') {
1375 options = LY_CTX_NOYANGLIBRARY;
1376 goto create_empty;
1377 } else {
1378 ylpath = strdup(&arg[i]);
1379 format = detect_data_format(ylpath);
1380 if (format == LYD_UNKNOWN) {
1381 free(ylpath);
1382 fprintf(stderr, "Unable to resolve format of the yang library file, please add \".xml\" or \".json\" suffix.\n");
1383 goto create_empty;
1384 }
1385 searchpaths = ly_ctx_get_searchdirs(ctx);
1386 ctx_new = ly_ctx_new_ylpath(searchpaths ? searchpaths[0] : NULL, ylpath, format, 0);
1387 free(ylpath);
1388 }
1389 } else {
1390create_empty:
1391#else
Radek Krejci92f5ce92019-09-06 16:25:43 +02001392 (void) arg; /* TODO yang-library support */
Radek Krejcied5acc52019-04-25 15:57:04 +02001393 {
1394#endif
1395 ly_ctx_new(NULL, options, &ctx_new);
1396 }
1397
1398 if (!ctx_new) {
1399 fprintf(stderr, "Failed to create context.\n");
1400 return 1;
1401 }
1402
1403 /* final switch */
1404 ly_ctx_destroy(ctx, NULL);
1405 ctx = ctx_new;
1406
1407 return 0;
1408}
1409
1410int
1411cmd_verb(const char *arg)
1412{
1413 const char *verb;
1414 if (strlen(arg) < 5) {
1415 cmd_verb_help();
1416 return 1;
1417 }
1418
1419 verb = arg + 5;
1420 if (!strcmp(verb, "error") || !strcmp(verb, "0")) {
1421 ly_verb(LY_LLERR);
1422#ifndef NDEBUG
1423 ly_verb_dbg(0);
1424#endif
1425 } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) {
1426 ly_verb(LY_LLWRN);
1427#ifndef NDEBUG
1428 ly_verb_dbg(0);
1429#endif
1430 } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) {
1431 ly_verb(LY_LLVRB);
1432#ifndef NDEBUG
1433 ly_verb_dbg(0);
1434#endif
1435 } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) {
1436 ly_verb(LY_LLDBG);
1437#ifndef NDEBUG
1438 ly_verb_dbg(LY_LDGDICT | LY_LDGYANG | LY_LDGYIN | LY_LDGXPATH | LY_LDGDIFF);
1439#endif
1440 } else {
1441 fprintf(stderr, "Unknown verbosity \"%s\"\n", verb);
1442 return 1;
1443 }
1444
1445 return 0;
1446}
1447
1448#ifndef NDEBUG
1449
1450int
1451cmd_debug(const char *arg)
1452{
1453 const char *beg, *end;
1454 int grps = 0;
1455 if (strlen(arg) < 6) {
1456 cmd_debug_help();
1457 return 1;
1458 }
1459
1460 end = arg + 6;
1461 while (end[0]) {
1462 for (beg = end; isspace(beg[0]); ++beg);
1463 if (!beg[0]) {
1464 break;
1465 }
1466
1467 for (end = beg; (end[0] && !isspace(end[0])); ++end);
1468
1469 if (!strncmp(beg, "dict", end - beg)) {
1470 grps |= LY_LDGDICT;
1471 } else if (!strncmp(beg, "yang", end - beg)) {
1472 grps |= LY_LDGYANG;
1473 } else if (!strncmp(beg, "yin", end - beg)) {
1474 grps |= LY_LDGYIN;
1475 } else if (!strncmp(beg, "xpath", end - beg)) {
1476 grps |= LY_LDGXPATH;
1477 } else if (!strncmp(beg, "diff", end - beg)) {
1478 grps |= LY_LDGDIFF;
1479 } else {
1480 fprintf(stderr, "Unknown debug group \"%.*s\"\n", (int)(end - beg), beg);
1481 return 1;
1482 }
1483 }
1484 ly_verb_dbg(grps);
1485
1486 return 0;
1487}
1488
1489#endif
1490
1491int
1492cmd_quit(const char *UNUSED(arg))
1493{
1494 done = 1;
1495 return 0;
1496}
1497
1498int
1499cmd_help(const char *arg)
1500{
1501 int i;
1502 char *args = strdup(arg);
1503 char *cmd = NULL;
1504
1505 strtok(args, " ");
1506 if ((cmd = strtok(NULL, " ")) == NULL) {
1507
1508generic_help:
1509 fprintf(stdout, "Available commands:\n");
1510
1511 for (i = 0; commands[i].name; i++) {
1512 if (commands[i].helpstring != NULL) {
1513 fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring);
1514 }
1515 }
1516 } else {
1517 /* print specific help for the selected command */
1518
1519 /* get the command of the specified name */
1520 for (i = 0; commands[i].name; i++) {
1521 if (strcmp(cmd, commands[i].name) == 0) {
1522 break;
1523 }
1524 }
1525
1526 /* execute the command's help if any valid command specified */
1527 if (commands[i].name) {
1528 if (commands[i].help_func != NULL) {
1529 commands[i].help_func();
1530 } else {
1531 printf("%s\n", commands[i].helpstring);
1532 }
1533 } else {
1534 /* if unknown command specified, print the list of commands */
1535 printf("Unknown command \'%s\'\n", cmd);
1536 goto generic_help;
1537 }
1538 }
1539
1540 free(args);
1541 return 0;
1542}
1543
1544COMMAND commands[] = {
1545 {"help", cmd_help, NULL, "Display commands description"},
1546 {"add", cmd_add, cmd_add_help, "Add a new model from a specific file"},
1547 {"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
1548 {"print", cmd_print, cmd_print_help, "Print a model"},
Radek Krejcied5acc52019-04-25 15:57:04 +02001549 {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
Radek Krejcie7b95092019-05-15 11:03:07 +02001550#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001551 {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"},
1552 {"list", cmd_list, cmd_list_help, "List all the loaded models"},
1553#endif
1554 {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"},
1555 {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"},
1556 {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"},
1557 {"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
1558#ifndef NDEBUG
1559 {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
1560#endif
1561 {"quit", cmd_quit, NULL, "Quit the program"},
1562 /* synonyms for previous commands */
1563 {"?", cmd_help, NULL, "Display commands description"},
1564 {"exit", cmd_quit, NULL, "Quit the program"},
1565 {NULL, NULL, NULL, NULL}
1566};