blob: 690f0b02cacc3e2df1d53b32be8fa638cca57da2 [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{
Radek Krejcie58f97f2020-08-18 11:45:08 +0200189 int path_len, ret = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200190 char *path, *dir, *s, *arg_ptr;
Radek Krejcied5acc52019-04-25 15:57:04 +0200191 const struct lys_module *model;
192 LYS_INFORMAT format = LYS_IN_UNKNOWN;
193
194 if (strlen(arg) < 5) {
195 cmd_add_help();
196 return 1;
197 }
198
199 arg_ptr = strdup(arg + 3 /* ignore "add" */);
200
201 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
202 if (s[2] == '\0' || s[2] == ' ') {
Radek Krejci3fa46b62019-09-11 10:47:30 +0200203 ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200204 s[0] = s[1] = ' ';
205 }
206 }
207 s = arg_ptr;
208
209 while (arg_ptr[0] == ' ') {
210 ++arg_ptr;
211 }
212 if (strchr(arg_ptr, ' ')) {
213 path_len = strchr(arg_ptr, ' ') - arg_ptr;
214 } else {
215 path_len = strlen(arg_ptr);
216 }
217 path = strndup(arg_ptr, path_len);
218
Radek Krejcied5acc52019-04-25 15:57:04 +0200219 while (path) {
Radek Krejci83126492020-08-15 15:40:04 +0200220 int unset_path = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200221 format = get_schema_format(path);
222 if (format == LYS_IN_UNKNOWN) {
223 free(path);
224 goto cleanup;
225 }
226
Radek Krejci83126492020-08-15 15:40:04 +0200227 /* add temporarily also the path of the module itself */
Radek Krejcied5acc52019-04-25 15:57:04 +0200228 dir = strdup(path);
Radek Krejci83126492020-08-15 15:40:04 +0200229 if (ly_ctx_set_searchdir(ctx, dirname(dir)) == LY_EEXIST) {
230 unset_path = 0;
231 }
232 /* parse the file */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200233 lys_parse_path(ctx, path, format, &model);
Radek Krejcie58f97f2020-08-18 11:45:08 +0200234 ly_ctx_unset_searchdir_last(ctx, unset_path);
Radek Krejcied5acc52019-04-25 15:57:04 +0200235 free(path);
236 free(dir);
237
238 if (!model) {
239 /* libyang printed the error messages */
240 goto cleanup;
241 }
242
243 /* next model */
244 arg_ptr += path_len;
245 while (arg_ptr[0] == ' ') {
246 ++arg_ptr;
247 }
248 if (strchr(arg_ptr, ' ')) {
249 path_len = strchr(arg_ptr, ' ') - arg_ptr;
250 } else {
251 path_len = strlen(arg_ptr);
252 }
253
254 if (path_len) {
255 path = strndup(arg_ptr, path_len);
256 } else {
257 path = NULL;
258 }
259 }
260 if (format == LYS_IN_UNKNOWN) {
261 /* no schema on input */
262 cmd_add_help();
263 goto cleanup;
264 }
265 ret = 0;
266
267cleanup:
268 free(s);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200269 ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200270
271 return ret;
272}
273
274int
275cmd_load(const char *arg)
276{
277 int name_len, ret = 1;
278 char *name, *s, *arg_ptr;
279 const struct lys_module *model;
280
281 if (strlen(arg) < 6) {
282 cmd_load_help();
283 return 1;
284 }
285
286 arg_ptr = strdup(arg + 4 /* ignore "load" */);
287
288 for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
289 if (s[2] == '\0' || s[2] == ' ') {
Radek Krejci3fa46b62019-09-11 10:47:30 +0200290 ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200291 s[0] = s[1] = ' ';
292 }
293 }
294 s = arg_ptr;
295
296 while (arg_ptr[0] == ' ') {
297 ++arg_ptr;
298 }
299 if (strchr(arg_ptr, ' ')) {
300 name_len = strchr(arg_ptr, ' ') - arg_ptr;
301 } else {
302 name_len = strlen(arg_ptr);
303 }
304 name = strndup(arg_ptr, name_len);
305
306 while (name) {
307 model = ly_ctx_load_module(ctx, name, NULL);
308 free(name);
309 if (!model) {
310 /* libyang printed the error messages */
311 goto cleanup;
312 }
313
314 /* next model */
315 arg_ptr += name_len;
316 while (arg_ptr[0] == ' ') {
317 ++arg_ptr;
318 }
319 if (strchr(arg_ptr, ' ')) {
320 name_len = strchr(arg_ptr, ' ') - arg_ptr;
321 } else {
322 name_len = strlen(arg_ptr);
323 }
324
325 if (name_len) {
326 name = strndup(arg_ptr, name_len);
327 } else {
328 name = NULL;
329 }
330 }
331 ret = 0;
332
333cleanup:
334 free(s);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200335 ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED);
Radek Krejcied5acc52019-04-25 15:57:04 +0200336
337 return ret;
338}
339
340int
341cmd_print(const char *arg)
342{
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800343 int c, argc, option_index, ret = 1, tree_ll = 0, output_opts = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200344 char **argv = NULL, *ptr, *model_name, *revision;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800345 const char *out_path = NULL, *target_path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200346 const struct lys_module *module;
347 LYS_OUTFORMAT format = LYS_OUT_TREE;
Radek Krejci241f6b52020-05-21 18:13:49 +0200348 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200349 static struct option long_options[] = {
350 {"help", no_argument, 0, 'h'},
351 {"format", required_argument, 0, 'f'},
352 {"output", required_argument, 0, 'o'},
353#if 0
354 {"tree-print-groupings", no_argument, 0, 'g'},
355 {"tree-print-uses", no_argument, 0, 'u'},
356 {"tree-no-leafref-target", no_argument, 0, 'n'},
357 {"tree-path", required_argument, 0, 'P'},
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800358#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200359 {"info-path", required_argument, 0, 'P'},
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800360 {"single-node", no_argument, 0, 's'},
361#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200362 {"tree-line-length", required_argument, 0, 'L'},
363#endif
364 {NULL, 0, 0, 0}
365 };
366 void *rlcd;
367
368 argc = 1;
369 argv = malloc(2*sizeof *argv);
370 *argv = strdup(arg);
371 ptr = strtok(*argv, " ");
372 while ((ptr = strtok(NULL, " "))) {
373 rlcd = realloc(argv, (argc+2)*sizeof *argv);
374 if (!rlcd) {
375 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
376 goto cleanup;
377 }
378 argv = rlcd;
379 argv[argc++] = ptr;
380 }
381 argv[argc] = NULL;
382
383 optind = 0;
384 while (1) {
385 option_index = 0;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800386 c = getopt_long(argc, argv, "chf:go:guP:sL:", long_options, &option_index);
Radek Krejcied5acc52019-04-25 15:57:04 +0200387 if (c == -1) {
388 break;
389 }
390
391 switch (c) {
392 case 'h':
393 cmd_print_help();
394 ret = 0;
395 goto cleanup;
396 case 'f':
397 if (!strcmp(optarg, "yang")) {
398 format = LYS_OUT_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200399 } else if (!strcmp(optarg, "yin")) {
400 format = LYS_OUT_YIN;
FredGand944bdc2019-11-05 21:57:07 +0800401#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200402 } else if (!strcmp(optarg, "tree")) {
403 format = LYS_OUT_TREE;
404 } else if (!strcmp(optarg, "tree-rfc")) {
405 format = LYS_OUT_TREE;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800406 output_opts |= LYS_OUTOPT_TREE_RFC;
Radek Krejcied5acc52019-04-25 15:57:04 +0200407#endif
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800408 } else if (!strcmp(optarg, "info")) {
409 format = LYS_OUT_YANG_COMPILED;
Radek Krejcied5acc52019-04-25 15:57:04 +0200410 } else {
411 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
412 goto cleanup;
413 }
414 break;
415 case 'o':
416 if (out_path) {
417 fprintf(stderr, "Output specified twice.\n");
418 goto cleanup;
419 }
420 out_path = optarg;
421 break;
422#if 0
423 case 'g':
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800424 output_opts |= LYS_OUTOPT_TREE_GROUPING;
Radek Krejcied5acc52019-04-25 15:57:04 +0200425 break;
426 case 'u':
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800427 output_opts |= LYS_OUTOPT_TREE_USES;
Radek Krejcied5acc52019-04-25 15:57:04 +0200428 break;
429 case 'n':
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800430 output_opts |= LYS_OUTOPT_TREE_NO_LEAFREF;
Radek Krejcied5acc52019-04-25 15:57:04 +0200431 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800432#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200433 case 'P':
434 target_path = optarg;
435 break;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800436 case 's':
Radek Krejci4fa6ebf2019-11-21 13:34:35 +0800437 output_opts |= LYS_OUTPUT_NO_SUBSTMT;
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800438 break;
439#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200440 case 'L':
441 tree_ll = atoi(optarg);
442 break;
443#endif
444 case '?':
445 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
446 goto cleanup;
447 }
448 }
449
450 /* file name */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800451 if (optind == argc && !target_path) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200452 fprintf(stderr, "Missing the module name.\n");
453 goto cleanup;
454 }
455
Radek Krejci77954e82019-04-30 13:51:29 +0200456#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200457 /* tree fromat with or without gropings */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800458 if ((output_opts || tree_ll) && format != LYS_OUT_TREE) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200459 fprintf(stderr, "--tree options take effect only in case of the tree output format.\n");
460 }
Radek Krejci77954e82019-04-30 13:51:29 +0200461#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200462
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800463 if (!target_path) {
464 /* module, revision */
465 model_name = argv[optind];
466 revision = NULL;
467 if (strchr(model_name, '@')) {
468 revision = strchr(model_name, '@');
469 revision[0] = '\0';
470 ++revision;
471 }
472
473 if (revision) {
474 module = ly_ctx_get_module(ctx, model_name, revision);
475 } else {
476 module = ly_ctx_get_module_latest(ctx, model_name);
477 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200478#if 0
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800479 if (!module) {
480 /* not a module, try to find it as a submodule */
481 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
482 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200483#endif
484
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800485 if (!module) {
486 if (revision) {
487 fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
488 } else {
489 fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
490 }
491 goto cleanup;
Radek Krejcied5acc52019-04-25 15:57:04 +0200492 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200493 }
494
495 if (out_path) {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200496 ret = ly_out_new_filepath(out_path, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100497 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200498 ret = ly_out_new_file(stdout, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100499 }
Radek Krejci84ce7b12020-06-11 17:28:25 +0200500 if (ret) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100501 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
502 goto cleanup;
Radek Krejcied5acc52019-04-25 15:57:04 +0200503 }
504
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800505 if (target_path) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100506 const struct lysc_node *node = lys_find_node(ctx, NULL, target_path);
507 if (node) {
508 ret = lys_print_node(out, node, format, tree_ll, output_opts);
509 } else {
510 fprintf(stderr, "The requested schema node \"%s\" does not exists.\n", target_path);
511 }
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800512 } else {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200513 ret = lys_print_module(out, module, format, tree_ll, output_opts);
Radek Krejcied5acc52019-04-25 15:57:04 +0200514 }
515
516cleanup:
517 free(*argv);
518 free(argv);
519
Radek Krejci241f6b52020-05-21 18:13:49 +0200520 ly_out_free(out, NULL, out_path ? 1 : 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200521
522 return ret;
523}
Radek Krejcie7b95092019-05-15 11:03:07 +0200524
Radek Krejcied5acc52019-04-25 15:57:04 +0200525static int
Michal Vaskof03ed032020-03-04 13:31:44 +0100526parse_data(char *filepath, int *options, const struct lyd_node *tree, const char *rpc_act_file,
Radek Krejcied5acc52019-04-25 15:57:04 +0200527 struct lyd_node **result)
528{
Radek Krejcied5acc52019-04-25 15:57:04 +0200529 struct lyd_node *data = NULL, *rpc_act = NULL;
530 int opts = *options;
Radek Krejci7931b192020-06-25 17:05:03 +0200531 struct ly_in *in;
Radek Krejcied5acc52019-04-25 15:57:04 +0200532
Radek Krejci7931b192020-06-25 17:05:03 +0200533 if (ly_in_new_filepath(filepath, 0, &in)) {
534 fprintf(stderr, "Unable to open input YANG data file \"%s\".", filepath);
Radek Krejcied5acc52019-04-25 15:57:04 +0200535 return EXIT_FAILURE;
536 }
537
Radek Krejcie7b95092019-05-15 11:03:07 +0200538#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200539 if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) {
540 /* automatically detect data type from the data top level */
541 if (informat != LYD_XML) {
542 fprintf(stderr, "Only XML data can be automatically explored.\n");
543 return EXIT_FAILURE;
544 }
545
546 xml = lyxml_parse_path(ctx, filepath, 0);
547 if (!xml) {
548 fprintf(stderr, "Failed to parse XML data for automatic type detection.\n");
549 return EXIT_FAILURE;
550 }
551
552 /* NOTE: namespace is ignored to simplify usage of this feature */
553
554 if (!strcmp(xml->name, "data")) {
555 fprintf(stdout, "Parsing %s as complete datastore.\n", filepath);
556 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_ADD_YANGLIB;
557 } else if (!strcmp(xml->name, "config")) {
558 fprintf(stdout, "Parsing %s as config data.\n", filepath);
559 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
560 } else if (!strcmp(xml->name, "get-reply")) {
561 fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath);
562 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
563 } else if (!strcmp(xml->name, "get-config-reply")) {
564 fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath);
565 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
566 } else if (!strcmp(xml->name, "edit-config")) {
567 fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath);
568 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
569 } else if (!strcmp(xml->name, "rpc")) {
570 fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath);
571 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
572 } else if (!strcmp(xml->name, "rpc-reply")) {
573 if (!rpc_act_file) {
574 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
575 lyxml_free(ctx, xml);
576 return EXIT_FAILURE;
577 }
578 fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath);
579 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
580 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
581 if (!rpc_act) {
582 fprintf(stderr, "Failed to parse RPC/action.\n");
583 lyxml_free(ctx, xml);
584 return EXIT_FAILURE;
585 }
586 } else if (!strcmp(xml->name, "notification")) {
587 fprintf(stdout, "Parsing %s as <notification> data.\n", filepath);
588 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
589 } else if (!strcmp(xml->name, "yang-data")) {
590 fprintf(stdout, "Parsing %s as <yang-data> data.\n", filepath);
591 opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
592 if (!rpc_act_file) {
593 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
594 lyxml_free(ctx, xml);
595 return EXIT_FAILURE;
596 }
597 } else {
598 fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n");
599 lyxml_free(ctx, xml);
600 return EXIT_FAILURE;
601 }
602
603 if (opts & LYD_OPT_RPCREPLY) {
604 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act, val_tree);
605 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
606 data = lyd_parse_xml(ctx, &xml->child, opts, val_tree);
607 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
608 data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act_file);
609 } else {
610 data = lyd_parse_xml(ctx, &xml->child, opts);
611 }
612 lyxml_free(ctx, xml);
613 } else {
614 if (opts & LYD_OPT_RPCREPLY) {
615 if (!rpc_act_file) {
616 fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
617 return EXIT_FAILURE;
618 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200619 rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200620 if (!rpc_act) {
621 fprintf(stderr, "Failed to parse RPC/action.\n");
622 return EXIT_FAILURE;
623 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200624 if (trees) {
625 const struct lyd_node **trees_new;
626 unsigned int u;
627 trees_new = lyd_trees_new(1, rpc_act);
628
629 LY_ARRAY_FOR(trees, u) {
630 trees_new = lyd_trees_add(trees_new, trees[u]);
631 }
632 lyd_trees_free(trees, 0);
633 trees = trees_new;
634 } else {
635 trees = lyd_trees_new(1, rpc_act);
636 }
637 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200638 } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200639 data = lyd_parse_path(ctx, filepath, informat, opts, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200640 } else if (opts & LYD_OPT_DATA_TEMPLATE) {
641 if (!rpc_act_file) {
642 fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
643 return EXIT_FAILURE;
644 }
645 data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file);
646 } else {
Michal Vaskoa3881362020-01-21 15:57:35 +0100647#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200648
Radek Krejci5536d282020-08-04 23:27:44 +0200649 lyd_parse_data(ctx, in, 0, opts, LYD_VALIDATE_PRESENT, &data);
Radek Krejcie7b95092019-05-15 11:03:07 +0200650#if 0
Michal Vaskoa3881362020-01-21 15:57:35 +0100651 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200652 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200653#endif
Radek Krejci7931b192020-06-25 17:05:03 +0200654 ly_in_free(in, 0);
655
Radek Krejcie7b95092019-05-15 11:03:07 +0200656 lyd_free_all(rpc_act);
Radek Krejcied5acc52019-04-25 15:57:04 +0200657
Radek Krejcie7b95092019-05-15 11:03:07 +0200658 if (ly_err_first(ctx)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200659 fprintf(stderr, "Failed to parse data.\n");
Radek Krejcie7b95092019-05-15 11:03:07 +0200660 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200661 return EXIT_FAILURE;
662 }
663
664 *result = data;
665 *options = opts;
666 return EXIT_SUCCESS;
667}
668
669int
670cmd_data(const char *arg)
671{
672 int c, argc, option_index, ret = 1;
673 int options = 0, printopt = 0;
674 char **argv = NULL, *ptr;
675 const char *out_path = NULL;
Michal Vaskof03ed032020-03-04 13:31:44 +0100676 struct lyd_node *data = NULL;
677 struct lyd_node *tree = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100678 LYD_FORMAT outformat = 0;
Radek Krejci241f6b52020-05-21 18:13:49 +0200679 struct ly_out *out = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200680 static struct option long_options[] = {
681 {"defaults", required_argument, 0, 'd'},
682 {"help", no_argument, 0, 'h'},
683 {"format", required_argument, 0, 'f'},
684 {"option", required_argument, 0, 't'},
685 {"output", required_argument, 0, 'o'},
686 {"running", required_argument, 0, 'r'},
687 {"strict", no_argument, 0, 's'},
688 {NULL, 0, 0, 0}
689 };
690 void *rlcd;
691
692 argc = 1;
693 argv = malloc(2*sizeof *argv);
694 *argv = strdup(arg);
695 ptr = strtok(*argv, " ");
696 while ((ptr = strtok(NULL, " "))) {
697 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
698 if (!rlcd) {
699 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
700 goto cleanup;
701 }
702 argv = rlcd;
703 argv[argc++] = ptr;
704 }
705 argv[argc] = NULL;
706
707 optind = 0;
708 while (1) {
709 option_index = 0;
710 c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index);
711 if (c == -1) {
712 break;
713 }
714
715 switch (c) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200716#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200717 case 'd':
718 if (!strcmp(optarg, "all")) {
719 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
720 } else if (!strcmp(optarg, "all-tagged")) {
721 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
722 } else if (!strcmp(optarg, "trim")) {
723 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;
724 } else if (!strcmp(optarg, "implicit-tagged")) {
725 printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
726 }
727 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200728#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200729 case 'h':
730 cmd_data_help();
731 ret = 0;
732 goto cleanup;
733 case 'f':
734 if (!strcmp(optarg, "xml")) {
735 outformat = LYD_XML;
736 } else if (!strcmp(optarg, "json")) {
737 outformat = LYD_JSON;
738 } else if (!strcmp(optarg, "lyb")) {
739 outformat = LYD_LYB;
740 } else {
741 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
742 goto cleanup;
743 }
744 break;
745 case 'o':
746 if (out_path) {
747 fprintf(stderr, "Output specified twice.\n");
748 goto cleanup;
749 }
750 out_path = optarg;
751 break;
Michal Vaskoa3881362020-01-21 15:57:35 +0100752#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200753 case 'r':
Radek Krejcied5acc52019-04-25 15:57:04 +0200754 if (optarg[0] == '!') {
755 /* ignore extenral dependencies to the running datastore */
756 options |= LYD_OPT_NOEXTDEPS;
757 } else {
758 /* external file with the running datastore */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200759 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB, trees);
Radek Krejcied5acc52019-04-25 15:57:04 +0200760 if (!val_tree) {
761 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
762 goto cleanup;
763 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200764 if (!trees) {
765 trees = lyd_trees_new(1, val_tree);
766 } else {
767 trees = lyd_trees_add(trees, val_tree);
768 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200769 }
770 break;
771 case 's':
772 options |= LYD_OPT_STRICT;
773 options |= LYD_OPT_OBSOLETE;
774 break;
Michal Vaskoa3881362020-01-21 15:57:35 +0100775#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200776 case 't':
777 if (!strcmp(optarg, "auto")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100778 /* no flags */
Radek Krejcied5acc52019-04-25 15:57:04 +0200779 } else if (!strcmp(optarg, "data")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100780 /* no flags */
Michal Vaskob36053d2020-03-26 15:49:30 +0100781 /*} else if (!strcmp(optarg, "config")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100782 options |= LYD_OPT_CONFIG;
Radek Krejcied5acc52019-04-25 15:57:04 +0200783 } else if (!strcmp(optarg, "get")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100784 options |= LYD_OPT_GET;
Radek Krejcied5acc52019-04-25 15:57:04 +0200785 } else if (!strcmp(optarg, "getconfig")) {
Michal Vaskoa3881362020-01-21 15:57:35 +0100786 options |= LYD_OPT_GETCONFIG;
Michal Vaskob36053d2020-03-26 15:49:30 +0100787 } else if (!strcmp(optarg, "edit")) {
Michal Vasko9f96a052020-03-10 09:41:45 +0100788 options |= LYD_OPT_EDIT;*/
Radek Krejcied5acc52019-04-25 15:57:04 +0200789 } else {
790 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
791 cmd_data_help();
792 goto cleanup;
793 }
794 break;
795 case '?':
796 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
797 goto cleanup;
798 }
799 }
800
801 /* file name */
802 if (optind == argc) {
803 fprintf(stderr, "Missing the data file name.\n");
804 goto cleanup;
805 }
806
Michal Vaskof03ed032020-03-04 13:31:44 +0100807 if (parse_data(argv[optind], &options, tree, argv[optind + 1], &data)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200808 goto cleanup;
809 }
810
811 if (out_path) {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200812 ret = ly_out_new_filepath(out_path, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100813 } else {
Radek Krejci84ce7b12020-06-11 17:28:25 +0200814 ret = ly_out_new_file(stdout, &out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100815 }
Radek Krejci84ce7b12020-06-11 17:28:25 +0200816 if (ret) {
Radek Krejcia5bba312020-01-09 15:41:20 +0100817 fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
818 goto cleanup;
Radek Krejcied5acc52019-04-25 15:57:04 +0200819 }
820
Michal Vasko52927e22020-03-16 17:26:14 +0100821 if (outformat) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200822 ret = lyd_print_all(out, data, outformat, LYD_PRINT_FORMAT | printopt);
Radek Krejci84ce7b12020-06-11 17:28:25 +0200823 ret = ret < 0 ? ret * (-1) : 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200824 }
825
Radek Krejcied5acc52019-04-25 15:57:04 +0200826cleanup:
827 free(*argv);
828 free(argv);
829
Radek Krejci241f6b52020-05-21 18:13:49 +0200830 ly_out_free(out, NULL, out_path ? 1 : 0);
Radek Krejcied5acc52019-04-25 15:57:04 +0200831
Radek Krejcie7b95092019-05-15 11:03:07 +0200832 lyd_free_all(data);
Radek Krejcied5acc52019-04-25 15:57:04 +0200833
834 return ret;
835}
Radek Krejcie7b95092019-05-15 11:03:07 +0200836#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +0200837int
838cmd_xpath(const char *arg)
839{
840 int c, argc, option_index, ret = 1, long_str;
841 char **argv = NULL, *ptr, *expr = NULL;
842 unsigned int i, j;
843 int options = 0;
844 struct lyd_node *data = NULL, *node, *val_tree = NULL;
845 struct lyd_node_leaf_list *key;
846 struct ly_set *set;
847 static struct option long_options[] = {
848 {"help", no_argument, 0, 'h'},
849 {"expr", required_argument, 0, 'e'},
850 {NULL, 0, 0, 0}
851 };
852 void *rlcd;
853
854 long_str = 0;
855 argc = 1;
856 argv = malloc(2 * sizeof *argv);
857 *argv = strdup(arg);
858 ptr = strtok(*argv, " ");
859 while ((ptr = strtok(NULL, " "))) {
860 if (long_str) {
861 ptr[-1] = ' ';
862 if (ptr[strlen(ptr) - 1] == long_str) {
863 long_str = 0;
864 ptr[strlen(ptr) - 1] = '\0';
865 }
866 } else {
867 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
868 if (!rlcd) {
869 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
870 goto cleanup;
871 }
872 argv = rlcd;
873 argv[argc] = ptr;
874 if (ptr[0] == '"') {
875 long_str = '"';
876 ++argv[argc];
877 }
878 if (ptr[0] == '\'') {
879 long_str = '\'';
880 ++argv[argc];
881 }
882 if (ptr[strlen(ptr) - 1] == long_str) {
883 long_str = 0;
884 ptr[strlen(ptr) - 1] = '\0';
885 }
886 ++argc;
887 }
888 }
889 argv[argc] = NULL;
890
891 optind = 0;
892 while (1) {
893 option_index = 0;
894 c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index);
895 if (c == -1) {
896 break;
897 }
898
899 switch (c) {
900 case 'h':
901 cmd_xpath_help();
902 ret = 0;
903 goto cleanup;
904 case 'e':
905 expr = optarg;
906 break;
907 case 't':
908 if (!strcmp(optarg, "auto")) {
909 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
910 } else if (!strcmp(optarg, "config")) {
911 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
912 } else if (!strcmp(optarg, "get")) {
913 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
914 } else if (!strcmp(optarg, "getconfig")) {
915 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
916 } else if (!strcmp(optarg, "edit")) {
917 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
918 } else if (!strcmp(optarg, "rpc")) {
919 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
920 } else if (!strcmp(optarg, "rpcreply")) {
921 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
922 } else if (!strcmp(optarg, "notif")) {
923 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
924 } else if (!strcmp(optarg, "yangdata")) {
925 options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
926 } else {
927 fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
928 cmd_data_help();
929 goto cleanup;
930 }
931 break;
932 case 'x':
933 val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG);
934 if (!val_tree) {
935 fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
936 goto cleanup;
937 }
938 break;
939 case '?':
940 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
941 goto cleanup;
942 }
943 }
944
945 if (optind == argc) {
946 fprintf(stderr, "Missing the file with data.\n");
947 goto cleanup;
948 }
949
950 if (!expr) {
951 fprintf(stderr, "Missing the XPath expression.\n");
952 goto cleanup;
953 }
954
955 if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
956 goto cleanup;
957 }
958
959 if (!(set = lyd_find_path(data, expr))) {
960 goto cleanup;
961 }
962
963 /* print result */
964 printf("Result:\n");
965 if (!set->number) {
966 printf("\tEmpty\n");
967 } else {
968 for (i = 0; i < set->number; ++i) {
969 node = set->set.d[i];
970 switch (node->schema->nodetype) {
971 case LYS_CONTAINER:
972 printf("\tContainer ");
973 break;
974 case LYS_LEAF:
975 printf("\tLeaf ");
976 break;
977 case LYS_LEAFLIST:
978 printf("\tLeaflist ");
979 break;
980 case LYS_LIST:
981 printf("\tList ");
982 break;
983 case LYS_ANYXML:
984 printf("\tAnyxml ");
985 break;
986 case LYS_ANYDATA:
987 printf("\tAnydata ");
988 break;
989 default:
990 printf("\tUnknown ");
991 break;
992 }
993 printf("\"%s\"", node->schema->name);
994 if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
995 printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str);
996 } else if (node->schema->nodetype == LYS_LIST) {
997 key = (struct lyd_node_leaf_list *)node->child;
998 printf(" (");
999 for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) {
1000 if (j) {
1001 printf(" ");
1002 }
1003 printf("\"%s\": %s", key->schema->name, key->value_str);
1004 key = (struct lyd_node_leaf_list *)key->next;
1005 }
1006 printf(")");
1007 }
1008 printf("\n");
1009 }
1010 }
1011 printf("\n");
1012
1013 ly_set_free(set);
1014 ret = 0;
1015
1016cleanup:
1017 free(*argv);
1018 free(argv);
1019
1020 lyd_free_withsiblings(data);
1021
1022 return ret;
1023}
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001024#endif
Radek Krejcied5acc52019-04-25 15:57:04 +02001025
1026int
1027print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
1028{
1029 struct lyd_node *ylib;
1030 uint32_t idx = 0, has_modules = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +02001031 const struct lys_module *mod;
1032
1033 if (outformat != LYD_UNKNOWN) {
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001034 if (ly_ctx_get_yanglib_data(ctx, &ylib)) {
Radek Krejcied5acc52019-04-25 15:57:04 +02001035 fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n");
1036 return 1;
1037 }
1038
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001039 lyd_print_file(out, ylib, outformat, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_FORMAT);
1040 lyd_free_all(ylib);
Radek Krejcied5acc52019-04-25 15:57:04 +02001041 return 0;
1042 }
1043
1044 /* iterate schemas in context and provide just the basic info */
1045 fprintf(out, "List of the loaded models:\n");
1046 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
1047 has_modules++;
1048
1049 /* conformance print */
1050 if (mod->implemented) {
1051 fprintf(out, "\tI");
1052 } else {
1053 fprintf(out, "\ti");
1054 }
1055
1056 /* module print */
1057 fprintf(out, " %s", mod->name);
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001058 if (mod->revision) {
1059 fprintf(out, "@%s", mod->revision);
Radek Krejcied5acc52019-04-25 15:57:04 +02001060 }
1061
1062 /* submodules print */
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001063 if (mod->parsed && mod->parsed->includes) {
1064 uint64_t u = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +02001065 fprintf(out, " (");
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001066 LY_ARRAY_FOR(mod->parsed->includes, u) {
1067 fprintf(out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name);
1068 if (mod->parsed->includes[u].rev[0]) {
1069 fprintf(out, "@%s", mod->parsed->includes[u].rev);
Radek Krejcied5acc52019-04-25 15:57:04 +02001070 }
1071 }
1072 fprintf(out, ")");
1073 }
1074
1075 /* finish the line */
1076 fprintf(out, "\n");
1077 }
1078
1079 if (!has_modules) {
1080 fprintf(out, "\t(none)\n");
1081 }
1082
1083 return 0;
1084}
1085
1086int
1087cmd_list(const char *arg)
1088{
1089 char **argv = NULL, *ptr;
1090 int c, argc, option_index;
1091 LYD_FORMAT outformat = LYD_UNKNOWN;
1092 static struct option long_options[] = {
1093 {"help", no_argument, 0, 'h'},
1094 {"format", required_argument, 0, 'f'},
1095 {NULL, 0, 0, 0}
1096 };
1097 void *rlcd;
1098
1099 argc = 1;
1100 argv = malloc(2*sizeof *argv);
1101 *argv = strdup(arg);
1102 ptr = strtok(*argv, " ");
1103 while ((ptr = strtok(NULL, " "))) {
1104 rlcd = realloc(argv, (argc+2)*sizeof *argv);
1105 if (!rlcd) {
1106 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1107 goto error;
1108 }
1109 argv = rlcd;
1110 argv[argc++] = ptr;
1111 }
1112 argv[argc] = NULL;
1113
1114 optind = 0;
1115 while (1) {
1116 option_index = 0;
1117 c = getopt_long(argc, argv, "hf:", long_options, &option_index);
1118 if (c == -1) {
1119 break;
1120 }
1121
1122 switch (c) {
1123 case 'h':
1124 cmd_data_help();
1125 free(*argv);
1126 free(argv);
1127 return 0;
1128 case 'f':
1129 if (!strcmp(optarg, "xml")) {
1130 outformat = LYD_XML;
1131 } else if (!strcmp(optarg, "json")) {
1132 outformat = LYD_JSON;
1133 } else {
1134 fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
1135 goto error;
1136 }
1137 break;
1138 case '?':
1139 /* getopt_long() prints message */
1140 goto error;
1141 }
1142 }
1143 if (optind != argc) {
1144 fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]);
1145error:
1146 free(*argv);
1147 free(argv);
1148 return 1;
1149 }
1150 free(*argv);
1151 free(argv);
1152
1153 return print_list(stdout, ctx, outformat);
1154}
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001155
Radek Krejcied5acc52019-04-25 15:57:04 +02001156int
1157cmd_feature(const char *arg)
1158{
1159 int c, argc, option_index, ret = 1, task = 0;
1160 char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL;
1161 const struct lys_module *module;
1162 static struct option long_options[] = {
1163 {"help", no_argument, 0, 'h'},
1164 {"enable", required_argument, 0, 'e'},
1165 {"disable", required_argument, 0, 'd'},
1166 {NULL, 0, 0, 0}
1167 };
1168 void *rlcd;
1169
1170 argc = 1;
1171 argv = malloc(2*sizeof *argv);
1172 *argv = strdup(arg);
1173 ptr = strtok(*argv, " ");
1174 while ((ptr = strtok(NULL, " "))) {
1175 rlcd = realloc(argv, (argc + 2) * sizeof *argv);
1176 if (!rlcd) {
1177 fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
1178 goto cleanup;
1179 }
1180 argv = rlcd;
1181 argv[argc++] = ptr;
1182 }
1183 argv[argc] = NULL;
1184
1185 optind = 0;
1186 while (1) {
1187 option_index = 0;
1188 c = getopt_long(argc, argv, "he:d:", long_options, &option_index);
1189 if (c == -1) {
1190 break;
1191 }
1192
1193 switch (c) {
1194 case 'h':
1195 cmd_feature_help();
1196 ret = 0;
1197 goto cleanup;
1198 case 'e':
1199 if (task) {
1200 fprintf(stderr, "Only one of enable or disable can be specified.\n");
1201 goto cleanup;
1202 }
1203 task = 1;
1204 feat_names = optarg;
1205 break;
1206 case 'd':
1207 if (task) {
1208 fprintf(stderr, "Only one of enable, or disable can be specified.\n");
1209 goto cleanup;
1210 }
1211 task = 2;
1212 feat_names = optarg;
1213 break;
1214 case '?':
1215 fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
1216 goto cleanup;
1217 }
1218 }
1219
1220 /* module name */
1221 if (optind == argc) {
1222 fprintf(stderr, "Missing the module name.\n");
1223 goto cleanup;
1224 }
1225
1226 revision = NULL;
1227 model_name = argv[optind];
1228 if (strchr(model_name, '@')) {
1229 revision = strchr(model_name, '@');
1230 revision[0] = '\0';
1231 ++revision;
1232 }
1233
Radek Krejci3d46f612020-08-13 12:01:53 +02001234 if (!revision) {
1235 module = ly_ctx_get_module_implemented(ctx, model_name);
1236 } else {
1237 module = ly_ctx_get_module(ctx, model_name, revision);
1238 if (module && !module->implemented) {
1239 module = NULL;
1240 }
1241 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001242#if 0
1243 if (!module) {
1244 /* not a module, try to find it as a submodule */
1245 module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
1246 }
1247#endif
1248
1249 if (module == NULL) {
1250 if (revision) {
Radek Krejci3d46f612020-08-13 12:01:53 +02001251 fprintf(stderr, "No implemented (sub)module \"%s\" in revision %s found.\n", model_name, revision);
Radek Krejcied5acc52019-04-25 15:57:04 +02001252 } else {
Radek Krejci3d46f612020-08-13 12:01:53 +02001253 fprintf(stderr, "No implemented (sub)module \"%s\" found.\n", model_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001254 }
1255 goto cleanup;
1256 }
1257
1258 if (!task) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001259 size_t len, max_len = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001260 LY_ARRAY_COUNT_TYPE u;
Radek Krejcied5acc52019-04-25 15:57:04 +02001261 struct lysc_feature *features;
1262
1263 printf("%s features:\n", module->name);
1264
1265 if (module->compiled) {
1266 features = module->compiled->features;
1267 } else {
Michal Vasko33ff9422020-07-03 09:50:39 +02001268 features = module->dis_features;
Radek Krejcied5acc52019-04-25 15:57:04 +02001269 }
1270
1271 /* get the max len */
1272 LY_ARRAY_FOR(features, u) {
1273 len = strlen(features[u].name);
1274 if (len > max_len) {
1275 max_len = len;
1276 }
1277 }
1278
1279 LY_ARRAY_FOR(features, u) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001280 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 +02001281 }
1282 if (!u) {
1283 printf("\t(none)\n");
1284 }
1285 } else {
1286 feat_names = strtok(feat_names, ",");
1287 while (feat_names) {
1288 if (((task == 1) && lys_feature_enable(module, feat_names))
1289 || ((task == 2) && lys_feature_disable(module, feat_names))) {
1290 fprintf(stderr, "Feature \"%s\" not found.\n", feat_names);
1291 ret = 1;
1292 }
1293 feat_names = strtok(NULL, ",");
1294 }
1295 }
1296
1297cleanup:
1298 free(*argv);
1299 free(argv);
1300
1301 return ret;
1302}
1303
1304int
1305cmd_searchpath(const char *arg)
1306{
1307 const char *path;
1308 const char * const *searchpaths;
1309 int index;
1310 struct stat st;
1311
1312 for (path = strchr(arg, ' '); path && (path[0] == ' '); ++path);
1313 if (!path || (path[0] == '\0')) {
1314 searchpaths = ly_ctx_get_searchdirs(ctx);
1315 if (searchpaths) {
1316 for (index = 0; searchpaths[index]; index++) {
1317 fprintf(stdout, "%s\n", searchpaths[index]);
1318 }
1319 }
1320 return 0;
1321 }
1322
1323 if ((!strncmp(path, "-h", 2) && (path[2] == '\0' || path[2] == ' ')) ||
1324 (!strncmp(path, "--help", 6) && (path[6] == '\0' || path[6] == ' '))) {
1325 cmd_searchpath_help();
1326 return 0;
1327 } else if (!strncmp(path, "--clear", 7) && (path[7] == '\0' || path[7] == ' ')) {
Radek Krejcie58f97f2020-08-18 11:45:08 +02001328 ly_ctx_unset_searchdir(ctx, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +02001329 return 0;
1330 }
1331
1332 if (stat(path, &st) == -1) {
1333 fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno));
1334 return 1;
1335 }
1336 if (!S_ISDIR(st.st_mode)) {
1337 fprintf(stderr, "\"%s\" is not a directory.\n", path);
1338 return 1;
1339 }
1340
1341 ly_ctx_set_searchdir(ctx, path);
1342
1343 return 0;
1344}
1345
1346int
1347cmd_clear(const char *arg)
1348{
1349 struct ly_ctx *ctx_new;
1350 int options = 0;
1351#if 0
1352 int i;
1353 char *ylpath;
1354 const char * const *searchpaths;
1355 LYD_FORMAT format;
1356
1357 /* get optional yang library file name */
1358 for (i = 5; arg[i] && isspace(arg[i]); i++);
1359 if (arg[i]) {
1360 if (arg[i] == '-' && arg[i + 1] == 'e') {
1361 options = LY_CTX_NOYANGLIBRARY;
1362 goto create_empty;
1363 } else {
1364 ylpath = strdup(&arg[i]);
1365 format = detect_data_format(ylpath);
1366 if (format == LYD_UNKNOWN) {
1367 free(ylpath);
1368 fprintf(stderr, "Unable to resolve format of the yang library file, please add \".xml\" or \".json\" suffix.\n");
1369 goto create_empty;
1370 }
1371 searchpaths = ly_ctx_get_searchdirs(ctx);
1372 ctx_new = ly_ctx_new_ylpath(searchpaths ? searchpaths[0] : NULL, ylpath, format, 0);
1373 free(ylpath);
1374 }
1375 } else {
1376create_empty:
1377#else
Radek Krejci92f5ce92019-09-06 16:25:43 +02001378 (void) arg; /* TODO yang-library support */
Radek Krejcied5acc52019-04-25 15:57:04 +02001379 {
1380#endif
1381 ly_ctx_new(NULL, options, &ctx_new);
1382 }
1383
1384 if (!ctx_new) {
1385 fprintf(stderr, "Failed to create context.\n");
1386 return 1;
1387 }
1388
1389 /* final switch */
1390 ly_ctx_destroy(ctx, NULL);
1391 ctx = ctx_new;
1392
1393 return 0;
1394}
1395
1396int
1397cmd_verb(const char *arg)
1398{
1399 const char *verb;
1400 if (strlen(arg) < 5) {
1401 cmd_verb_help();
1402 return 1;
1403 }
1404
1405 verb = arg + 5;
1406 if (!strcmp(verb, "error") || !strcmp(verb, "0")) {
1407 ly_verb(LY_LLERR);
1408#ifndef NDEBUG
1409 ly_verb_dbg(0);
1410#endif
1411 } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) {
1412 ly_verb(LY_LLWRN);
1413#ifndef NDEBUG
1414 ly_verb_dbg(0);
1415#endif
1416 } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) {
1417 ly_verb(LY_LLVRB);
1418#ifndef NDEBUG
1419 ly_verb_dbg(0);
1420#endif
1421 } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) {
1422 ly_verb(LY_LLDBG);
1423#ifndef NDEBUG
1424 ly_verb_dbg(LY_LDGDICT | LY_LDGYANG | LY_LDGYIN | LY_LDGXPATH | LY_LDGDIFF);
1425#endif
1426 } else {
1427 fprintf(stderr, "Unknown verbosity \"%s\"\n", verb);
1428 return 1;
1429 }
1430
1431 return 0;
1432}
1433
1434#ifndef NDEBUG
1435
1436int
1437cmd_debug(const char *arg)
1438{
1439 const char *beg, *end;
1440 int grps = 0;
1441 if (strlen(arg) < 6) {
1442 cmd_debug_help();
1443 return 1;
1444 }
1445
1446 end = arg + 6;
1447 while (end[0]) {
1448 for (beg = end; isspace(beg[0]); ++beg);
1449 if (!beg[0]) {
1450 break;
1451 }
1452
1453 for (end = beg; (end[0] && !isspace(end[0])); ++end);
1454
1455 if (!strncmp(beg, "dict", end - beg)) {
1456 grps |= LY_LDGDICT;
1457 } else if (!strncmp(beg, "yang", end - beg)) {
1458 grps |= LY_LDGYANG;
1459 } else if (!strncmp(beg, "yin", end - beg)) {
1460 grps |= LY_LDGYIN;
1461 } else if (!strncmp(beg, "xpath", end - beg)) {
1462 grps |= LY_LDGXPATH;
1463 } else if (!strncmp(beg, "diff", end - beg)) {
1464 grps |= LY_LDGDIFF;
1465 } else {
1466 fprintf(stderr, "Unknown debug group \"%.*s\"\n", (int)(end - beg), beg);
1467 return 1;
1468 }
1469 }
1470 ly_verb_dbg(grps);
1471
1472 return 0;
1473}
1474
1475#endif
1476
1477int
1478cmd_quit(const char *UNUSED(arg))
1479{
1480 done = 1;
1481 return 0;
1482}
1483
1484int
1485cmd_help(const char *arg)
1486{
1487 int i;
1488 char *args = strdup(arg);
1489 char *cmd = NULL;
1490
1491 strtok(args, " ");
1492 if ((cmd = strtok(NULL, " ")) == NULL) {
1493
1494generic_help:
1495 fprintf(stdout, "Available commands:\n");
1496
1497 for (i = 0; commands[i].name; i++) {
1498 if (commands[i].helpstring != NULL) {
1499 fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring);
1500 }
1501 }
1502 } else {
1503 /* print specific help for the selected command */
1504
1505 /* get the command of the specified name */
1506 for (i = 0; commands[i].name; i++) {
1507 if (strcmp(cmd, commands[i].name) == 0) {
1508 break;
1509 }
1510 }
1511
1512 /* execute the command's help if any valid command specified */
1513 if (commands[i].name) {
1514 if (commands[i].help_func != NULL) {
1515 commands[i].help_func();
1516 } else {
1517 printf("%s\n", commands[i].helpstring);
1518 }
1519 } else {
1520 /* if unknown command specified, print the list of commands */
1521 printf("Unknown command \'%s\'\n", cmd);
1522 goto generic_help;
1523 }
1524 }
1525
1526 free(args);
1527 return 0;
1528}
1529
1530COMMAND commands[] = {
1531 {"help", cmd_help, NULL, "Display commands description"},
1532 {"add", cmd_add, cmd_add_help, "Add a new model from a specific file"},
1533 {"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
1534 {"print", cmd_print, cmd_print_help, "Print a model"},
Radek Krejcied5acc52019-04-25 15:57:04 +02001535 {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
Radek Krejcie7b95092019-05-15 11:03:07 +02001536#if 0
Radek Krejcied5acc52019-04-25 15:57:04 +02001537 {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"},
Radek Krejcied5acc52019-04-25 15:57:04 +02001538#endif
Radek Krejci2a7a39e2020-08-21 10:39:17 +02001539 {"list", cmd_list, cmd_list_help, "List all the loaded models"},
Radek Krejcied5acc52019-04-25 15:57:04 +02001540 {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"},
1541 {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"},
1542 {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"},
1543 {"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
1544#ifndef NDEBUG
1545 {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
1546#endif
1547 {"quit", cmd_quit, NULL, "Quit the program"},
1548 /* synonyms for previous commands */
1549 {"?", cmd_help, NULL, "Display commands description"},
1550 {"exit", cmd_quit, NULL, "Quit the program"},
1551 {NULL, NULL, NULL, NULL}
1552};