forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
1500 lines (1356 loc) · 47 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file commands.c
* @author Michal Vasko <[email protected]>
* @brief libyang's yanglint tool commands
*
* Copyright (c) 2015 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include "commands.h"
#include "libyang.h"
#include "../../src/tree_schema.h"
#include "../../src/tree_data.h"
#include "../../src/xpath.h"
COMMAND commands[];
extern int done;
extern struct ly_ctx *ctx;
void
cmd_add_help(void)
{
printf("add [-i] <path-to-model> [<other-models> ...]\n");
printf("\t-i - make all the imported modules implemented\n");
}
void
cmd_clear_help(void)
{
printf("clear [<yang-library> | -e]\n");
printf("\t Replace the current context with an empty one, searchpaths are not kept.\n");
printf("\t If <yang-library> path specified, load the modules according to the yang library data.\n");
printf("\t Option '-e' causes ietf-yang-library will not be loaded.\n");
}
void
cmd_print_help(void)
{
printf("print [-f (yang | yin | tree [<tree-options>] | info [-t <info-path>])] [-o <output-file>]"
" <model-name>[@<revision>]\n");
printf("\n");
printf("\ttree-options:\t--tree-print-groupings\t(print top-level groupings in a separate section)\n");
printf("\t \t--tree-print-uses\t(print uses nodes instead the resolved grouping nodes)\n");
printf("\t \t--tree-no-leafref-target\t(do not print the target nodes of leafrefs)\n");
printf("\t \t--tree-path <schema-path>\t(print only the specified subtree)\n");
printf("\t \t--tree-line-length <line-length>\t(wrap lines if longer than line-length,\n");
printf("\t \t\tnot a strict limit, longer lines can often appear)\n");
printf("\n");
printf("\tinfo-path:\t<schema-path> | typedef[<schema-path>]/<typedef-name> |\n");
printf("\t \t| identity/<identity-name> | feature/<feature-name> |\n");
printf("\t \t| grouping/<grouping-name>(<schema-path>) |\n");
printf("\t \t| type/<schema-path-leaf-or-leaflist>\n");
printf("\n");
printf("\tschema-path:\t( /<module-name>:<node-identifier> )+\n");
}
void
cmd_data_help(void)
{
printf("data [-(-s)trict] [-t TYPE] [-d DEFAULTS] [-o <output-file>] [-f (xml | json)] [-r <running-file-name>]\n");
printf(" <data-file-name> [<RPC/action-data-file-name> | <yang-data name>]\n\n");
printf("Accepted TYPEs:\n");
printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n");
printf("\t this option is applicable only in case of XML input data.\n");
printf("\tdata - LYD_OPT_DATA (default value) - complete datastore including status data.\n");
printf("\tconfig - LYD_OPT_CONFIG - complete configuration datastore.\n");
printf("\tget - LYD_OPT_GET - <get> operation result.\n");
printf("\tgetconfig - LYD_OPT_GETCONFIG - <get-config> operation result.\n");
printf("\tedit - LYD_OPT_EDIT - <edit-config>'s data (content of its <config> element).\n");
printf("\trpc - LYD_OPT_RPC - NETCONF RPC message.\n");
printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n");
printf("\tnotif - LYD_OPT_NOTIF - NETCONF Notification message.\n");
printf("\tyangdata - LYD_OPT_DATA_TEMPLATE - yang-data extension (last parameter mandatory in this case)\n\n");
printf("Accepted DEFAULTS:\n");
printf("\tall - add missing default nodes\n");
printf("\tall-tagged - add missing default nodes and mark all the default nodes with the attribute.\n");
printf("\ttrim - remove all nodes with a default value\n");
printf("\timplicit-tagged - add missing nodes and mark them with the attribute\n\n");
printf("Option -r:\n");
printf("\tOptional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the file contains running\n");
printf("\tconfiguration datastore data referenced from the RPC/Notification. Note that the file is\n");
printf("\tvalidated as 'data' TYPE. Special value '!' can be used as argument to ignore the\n");
printf("\texternal references.\n\n");
printf("\tIf an XPath expression (when/must) needs access to configuration data, you can provide\n");
printf("\tthem in a file, which will be parsed as 'data' TYPE.\n\n");
}
void
cmd_xpath_help(void)
{
printf("xpath [-t TYPE] [-x <additional-tree-file-name>] -e <XPath-expression>\n"
" <XML-data-file-name> [<JSON-rpc/action-schema-nodeid>]\n");
printf("Accepted TYPEs:\n");
printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n");
printf("\t this option is applicable only in case of XML input data.\n");
printf("\tconfig - LYD_OPT_CONFIG\n");
printf("\tget - LYD_OPT_GET\n");
printf("\tgetconfig - LYD_OPT_GETCONFIG\n");
printf("\tedit - LYD_OPT_EDIT\n");
printf("\trpc - LYD_OPT_RPC\n");
printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n");
printf("\tnotif - LYD_OPT_NOTIF\n\n");
printf("Option -x:\n");
printf("\tIf RPC/action/notification/RPC reply (for TYPEs 'rpc', 'rpcreply', and 'notif') includes\n");
printf("\tan XPath expression (when/must) that needs access to the configuration data, you can provide\n");
printf("\tthem in a file, which will be parsed as 'config'.\n");
}
void
cmd_list_help(void)
{
printf("list [-f (xml | json)]\n\n");
printf("\tBasic list output (no -f): i - imported module, I - implemented module\n");
}
void
cmd_feature_help(void)
{
printf("feature [ -(-e)nable | -(-d)isable (* | <feature-name>[,<feature-name> ...]) ] <model-name>[@<revision>]\n");
}
void
cmd_searchpath_help(void)
{
printf("searchpath <model-dir-path> | --clear\n");
}
void
cmd_verb_help(void)
{
printf("verb (error/0 | warning/1 | verbose/2 | debug/3)\n");
}
#ifndef NDEBUG
void
cmd_debug_help(void)
{
printf("debug (dict | yang | yin | xpath | diff)+\n");
}
#endif
LYS_INFORMAT
get_schema_format(const char *path)
{
char *ptr;
if ((ptr = strrchr(path, '.')) != NULL) {
++ptr;
if (!strcmp(ptr, "yin")) {
return LYS_IN_YIN;
} else if (!strcmp(ptr, "yang")) {
return LYS_IN_YANG;
} else {
fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr);
return LYS_IN_UNKNOWN;
}
} else {
fprintf(stdout, "Input file \"%s\" without extension - unknown format.\n", path);
return LYS_IN_UNKNOWN;
}
}
int
cmd_add(const char *arg)
{
int path_len, ret = 1, index = 0;
char *path, *dir, *s, *arg_ptr;
const char * const *searchpaths;
const struct lys_module *model;
LYS_INFORMAT format = LYS_IN_UNKNOWN;
if (strlen(arg) < 5) {
cmd_add_help();
return 1;
}
arg_ptr = strdup(arg + 3 /* ignore "add" */);
for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) {
if (s[2] == '\0' || s[2] == ' ') {
ly_ctx_set_allimplemented(ctx);
s[0] = s[1] = ' ';
}
}
s = arg_ptr;
while (arg_ptr[0] == ' ') {
++arg_ptr;
}
if (strchr(arg_ptr, ' ')) {
path_len = strchr(arg_ptr, ' ') - arg_ptr;
} else {
path_len = strlen(arg_ptr);
}
path = strndup(arg_ptr, path_len);
searchpaths = ly_ctx_get_searchdirs(ctx);
if (searchpaths) {
for (index = 0; searchpaths[index]; index++);
}
while (path) {
format = get_schema_format(path);
if (format == LYS_IN_UNKNOWN) {
free(path);
goto cleanup;
}
dir = strdup(path);
ly_ctx_set_searchdir(ctx, dirname(dir));
model = lys_parse_path(ctx, path, format);
ly_ctx_unset_searchdirs(ctx, index);
free(path);
free(dir);
if (!model) {
/* libyang printed the error messages */
goto cleanup;
}
/* next model */
arg_ptr += path_len;
while (arg_ptr[0] == ' ') {
++arg_ptr;
}
if (strchr(arg_ptr, ' ')) {
path_len = strchr(arg_ptr, ' ') - arg_ptr;
} else {
path_len = strlen(arg_ptr);
}
if (path_len) {
path = strndup(arg_ptr, path_len);
} else {
path = NULL;
}
}
if (format == LYS_IN_UNKNOWN) {
/* no schema on input */
cmd_add_help();
goto cleanup;
}
ret = 0;
cleanup:
free(s);
ly_ctx_unset_allimplemented(ctx);
return ret;
}
int
cmd_print(const char *arg)
{
int c, argc, option_index, ret = 1, tree_ll = 0, tree_opts = 0;
char **argv = NULL, *ptr, *target_path = NULL, *model_name, *revision;
const char *out_path = NULL;
const struct lys_module *module;
LYS_OUTFORMAT format = LYS_OUT_TREE;
FILE *output = stdout;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"format", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{"tree-print-groupings", no_argument, 0, 'g'},
{"tree-print-uses", no_argument, 0, 'u'},
{"tree-no-leafref-target", no_argument, 0, 'n'},
{"tree-path", required_argument, 0, 'P'},
{"info-path", required_argument, 0, 'P'},
{"tree-line-length", required_argument, 0, 'L'},
{NULL, 0, 0, 0}
};
void *rlcd;
argc = 1;
argv = malloc(2*sizeof *argv);
*argv = strdup(arg);
ptr = strtok(*argv, " ");
while ((ptr = strtok(NULL, " "))) {
rlcd = realloc(argv, (argc+2)*sizeof *argv);
if (!rlcd) {
fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
goto cleanup;
}
argv = rlcd;
argv[argc++] = ptr;
}
argv[argc] = NULL;
optind = 0;
while (1) {
option_index = 0;
c = getopt_long(argc, argv, "hf:go:guP:L:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'h':
cmd_print_help();
ret = 0;
goto cleanup;
case 'f':
if (!strcmp(optarg, "yang")) {
format = LYS_OUT_YANG;
} else if (!strcmp(optarg, "yin")) {
format = LYS_OUT_YIN;
} else if (!strcmp(optarg, "tree")) {
format = LYS_OUT_TREE;
} else if (!strcmp(optarg, "tree-rfc")) {
format = LYS_OUT_TREE;
tree_opts |= LYS_OUTOPT_TREE_RFC;
} else if (!strcmp(optarg, "info")) {
format = LYS_OUT_INFO;
} else {
fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
goto cleanup;
}
break;
case 'o':
if (out_path) {
fprintf(stderr, "Output specified twice.\n");
goto cleanup;
}
out_path = optarg;
break;
case 'g':
tree_opts |= LYS_OUTOPT_TREE_GROUPING;
break;
case 'u':
tree_opts |= LYS_OUTOPT_TREE_USES;
break;
case 'n':
tree_opts |= LYS_OUTOPT_TREE_NO_LEAFREF;
break;
case 'P':
target_path = optarg;
break;
case 'L':
tree_ll = atoi(optarg);
break;
case '?':
fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
goto cleanup;
}
}
/* file name */
if (optind == argc) {
fprintf(stderr, "Missing the module name.\n");
goto cleanup;
}
/* tree fromat with or without gropings */
if ((tree_opts || tree_ll) && format != LYS_OUT_TREE) {
fprintf(stderr, "--tree options take effect only in case of the tree output format.\n");
}
/* module, revision */
model_name = argv[optind];
revision = NULL;
if (strchr(model_name, '@')) {
revision = strchr(model_name, '@');
revision[0] = '\0';
++revision;
}
module = ly_ctx_get_module(ctx, model_name, revision, 0);
if (!module) {
/* not a module, try to find it as a submodule */
module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision);
}
if (!module) {
if (revision) {
fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision);
} else {
fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name);
}
goto cleanup;
}
if (out_path) {
output = fopen(out_path, "w");
if (!output) {
fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
goto cleanup;
}
}
ret = lys_print_file(output, module, format, target_path, tree_ll, tree_opts);
cleanup:
free(*argv);
free(argv);
if (output && (output != stdout)) {
fclose(output);
}
return ret;
}
static LYD_FORMAT
detect_data_format(char *filepath)
{
size_t len;
/* detect input format according to file suffix */
len = strlen(filepath);
for (; isspace(filepath[len - 1]); len--, filepath[len] = '\0'); /* remove trailing whitespaces */
if (len >= 5 && !strcmp(&filepath[len - 4], ".xml")) {
return LYD_XML;
} else if (len >= 6 && !strcmp(&filepath[len - 5], ".json")) {
return LYD_JSON;
} else {
return LYD_UNKNOWN;
}
}
static int
parse_data(char *filepath, int *options, struct lyd_node *val_tree, const char *rpc_act_file,
struct lyd_node **result)
{
LYD_FORMAT informat = LYD_UNKNOWN;
struct lyxml_elem *xml = NULL;
struct lyd_node *data = NULL, *root, *next, *iter, *rpc_act = NULL;
void *lydval_arg = NULL;
int opts = *options;
/* detect input format according to file suffix */
informat = detect_data_format(filepath);
if (informat == LYD_UNKNOWN) {
fprintf(stderr, "Unable to resolve format of the input file, please add \".xml\" or \".json\" suffix.\n");
return EXIT_FAILURE;
}
ly_errno = LY_SUCCESS;
if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) {
/* automatically detect data type from the data top level */
if (informat != LYD_XML) {
fprintf(stderr, "Only XML data can be automatically explored.\n");
return EXIT_FAILURE;
}
xml = lyxml_parse_path(ctx, filepath, 0);
if (!xml) {
fprintf(stderr, "Failed to parse XML data for automatic type detection.\n");
return EXIT_FAILURE;
}
/* NOTE: namespace is ignored to simplify usage of this feature */
if (!strcmp(xml->name, "data")) {
fprintf(stdout, "Parsing %s as complete datastore.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK);
lydval_arg = ctx;
} else if (!strcmp(xml->name, "config")) {
fprintf(stdout, "Parsing %s as config data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
lydval_arg = ctx;
} else if (!strcmp(xml->name, "get-reply")) {
fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
lydval_arg = ctx;
} else if (!strcmp(xml->name, "get-config-reply")) {
fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
lydval_arg = ctx;
} else if (!strcmp(xml->name, "edit-config")) {
fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
lydval_arg = ctx;
} else if (!strcmp(xml->name, "rpc")) {
fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
} else if (!strcmp(xml->name, "rpc-reply")) {
if (!rpc_act_file) {
fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
lyxml_free(ctx, xml);
return EXIT_FAILURE;
}
fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
if (!rpc_act) {
fprintf(stderr, "Failed to parse RPC/action.\n");
lyxml_free(ctx, xml);
return EXIT_FAILURE;
}
} else if (!strcmp(xml->name, "notification")) {
fprintf(stdout, "Parsing %s as <notification> data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
} else if (!strcmp(xml->name, "yang-data")) {
fprintf(stdout, "Parsing %s as <yang-data> data.\n", filepath);
opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
if (!rpc_act_file) {
fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
lyxml_free(ctx, xml);
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n");
lyxml_free(ctx, xml);
return EXIT_FAILURE;
}
if (opts & LYD_OPT_RPCREPLY) {
data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act, val_tree);
} else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
data = lyd_parse_xml(ctx, &xml->child, opts, val_tree);
} else if (opts & LYD_OPT_DATA_TEMPLATE) {
data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act_file);
} else {
if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_DATA && !(opts & LYD_OPT_STRICT )) {
/* we have to include status data from ietf-yang-library which is part of the context,
* so we have to postpone validation after merging input data with ly_ctx_info() */
opts |= LYD_OPT_TRUSTED;
}
data = lyd_parse_xml(ctx, &xml->child, opts);
}
lyxml_free(ctx, xml);
} else {
if (opts & LYD_OPT_RPCREPLY) {
if (!rpc_act_file) {
fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n");
return EXIT_FAILURE;
}
rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree);
if (!rpc_act) {
fprintf(stderr, "Failed to parse RPC/action.\n");
return EXIT_FAILURE;
}
data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act, val_tree);
} else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
data = lyd_parse_path(ctx, filepath, informat, opts, val_tree);
} else if (opts & LYD_OPT_DATA_TEMPLATE) {
if (!rpc_act_file) {
fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n");
return EXIT_FAILURE;
}
data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file);
} else {
if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_DATA && !(opts & LYD_OPT_STRICT )) {
/* we have to include status data from ietf-yang-library which is part of the context,
* so we have to postpone validation after merging input data with ly_ctx_info() */
opts |= LYD_OPT_TRUSTED;
}
data = lyd_parse_path(ctx, filepath, informat, opts);
}
}
lyd_free_withsiblings(rpc_act);
if (ly_errno) {
fprintf(stderr, "Failed to parse data.\n");
lyd_free_withsiblings(data);
return EXIT_FAILURE;
}
if (opts & LYD_OPT_TRUSTED) {
/* postponed validation in case of LYD_OPT_DATA */
/* remove the trusted flag */
opts &= ~LYD_OPT_TRUSTED;
/* merge with ietf-yang-library */
root = ly_ctx_info(ctx);
if (lyd_merge(data, root, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) {
fprintf(stderr, "Merging input data with ietf-yang-library failed.\n");
lyd_free_withsiblings(root);
lyd_free_withsiblings(data);
return EXIT_FAILURE;
}
/* invalidate all data - do not believe to the source */
LY_TREE_FOR(data, root) {
LY_TREE_DFS_BEGIN(root, next, iter) {
iter->validity = LYD_VAL_OK;
switch (iter->schema->nodetype) {
case LYS_LEAFLIST:
case LYS_LEAF:
if (((struct lys_node_leaf *)iter->schema)->type.base == LY_TYPE_LEAFREF) {
iter->validity |= LYD_VAL_LEAFREF;
}
break;
case LYS_LIST:
iter->validity |= LYD_VAL_UNIQUE;
/* fallthrough */
case LYS_CONTAINER:
case LYS_NOTIF:
case LYS_RPC:
case LYS_ACTION:
iter->validity |= LYD_VAL_MAND;
break;
default:
break;
}
LY_TREE_DFS_END(root, next, iter)
}
}
/* validate the result */
if (lyd_validate(&data, opts, lydval_arg)) {
fprintf(stderr, "Failed to parse data.\n");
lyd_free_withsiblings(data);
return EXIT_FAILURE;
}
}
*result = data;
*options = opts;
return EXIT_SUCCESS;
}
int
cmd_data(const char *arg)
{
int c, argc, option_index, ret = 1;
int options = 0, printopt = 0;
char **argv = NULL, *ptr;
const char *out_path = NULL;
struct lyd_node *data = NULL, *val_tree = NULL;
LYD_FORMAT outformat = LYD_UNKNOWN;
FILE *output = stdout;
static struct option long_options[] = {
{"defaults", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{"format", required_argument, 0, 'f'},
{"option", required_argument, 0, 't'},
{"output", required_argument, 0, 'o'},
{"running", required_argument, 0, 'r'},
{"strict", no_argument, 0, 's'},
{NULL, 0, 0, 0}
};
void *rlcd;
argc = 1;
argv = malloc(2*sizeof *argv);
*argv = strdup(arg);
ptr = strtok(*argv, " ");
while ((ptr = strtok(NULL, " "))) {
rlcd = realloc(argv, (argc + 2) * sizeof *argv);
if (!rlcd) {
fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
goto cleanup;
}
argv = rlcd;
argv[argc++] = ptr;
}
argv[argc] = NULL;
optind = 0;
while (1) {
option_index = 0;
c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'd':
if (!strcmp(optarg, "all")) {
printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL;
} else if (!strcmp(optarg, "all-tagged")) {
printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG;
} else if (!strcmp(optarg, "trim")) {
printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM;
} else if (!strcmp(optarg, "implicit-tagged")) {
printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG;
}
break;
case 'h':
cmd_data_help();
ret = 0;
goto cleanup;
case 'f':
if (!strcmp(optarg, "xml")) {
outformat = LYD_XML;
} else if (!strcmp(optarg, "json")) {
outformat = LYD_JSON;
} else {
fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
goto cleanup;
}
break;
case 'o':
if (out_path) {
fprintf(stderr, "Output specified twice.\n");
goto cleanup;
}
out_path = optarg;
break;
case 'r':
if (val_tree || (options & LYD_OPT_NOEXTDEPS)) {
fprintf(stderr, "The running datastore (-r) cannot be set multiple times.\n");
goto cleanup;
}
if (optarg[0] == '!') {
/* ignore extenral dependencies to the running datastore */
options |= LYD_OPT_NOEXTDEPS;
} else {
/* external file with the running datastore */
val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB);
if (!val_tree) {
fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
goto cleanup;
}
}
break;
case 's':
options |= LYD_OPT_STRICT;
options |= LYD_OPT_OBSOLETE;
break;
case 't':
if (!strcmp(optarg, "auto")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
} else if (!strcmp(optarg, "data")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA;
} else if (!strcmp(optarg, "config")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
} else if (!strcmp(optarg, "get")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
} else if (!strcmp(optarg, "getconfig")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
} else if (!strcmp(optarg, "edit")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
} else if (!strcmp(optarg, "rpc")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
} else if (!strcmp(optarg, "rpcreply")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
} else if (!strcmp(optarg, "notif")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
} else if (!strcmp(optarg, "yangdata")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
} else {
fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
cmd_data_help();
goto cleanup;
}
break;
case '?':
fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
goto cleanup;
}
}
/* file name */
if (optind == argc) {
fprintf(stderr, "Missing the data file name.\n");
goto cleanup;
}
if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
goto cleanup;
}
if (out_path) {
output = fopen(out_path, "w");
if (!output) {
fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
goto cleanup;
}
}
if (outformat != LYD_UNKNOWN) {
if (options & LYD_OPT_RPCREPLY) {
lyd_print_file(output, data->child, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt);
} else {
lyd_print_file(output, data, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt);
}
}
ret = 0;
cleanup:
free(*argv);
free(argv);
if (output && (output != stdout)) {
fclose(output);
}
lyd_free_withsiblings(val_tree);
lyd_free_withsiblings(data);
return ret;
}
int
cmd_xpath(const char *arg)
{
int c, argc, option_index, ret = 1, long_str;
char **argv = NULL, *ptr, *expr = NULL;
unsigned int i, j;
int options = 0;
struct lyd_node *data = NULL, *node, *val_tree = NULL;
struct lyd_node_leaf_list *key;
struct ly_set *set;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"expr", required_argument, 0, 'e'},
{NULL, 0, 0, 0}
};
void *rlcd;
long_str = 0;
argc = 1;
argv = malloc(2 * sizeof *argv);
*argv = strdup(arg);
ptr = strtok(*argv, " ");
while ((ptr = strtok(NULL, " "))) {
if (long_str) {
ptr[-1] = ' ';
if (ptr[strlen(ptr) - 1] == long_str) {
long_str = 0;
ptr[strlen(ptr) - 1] = '\0';
}
} else {
rlcd = realloc(argv, (argc + 2) * sizeof *argv);
if (!rlcd) {
fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
goto cleanup;
}
argv = rlcd;
argv[argc] = ptr;
if (ptr[0] == '"') {
long_str = '"';
++argv[argc];
}
if (ptr[0] == '\'') {
long_str = '\'';
++argv[argc];
}
if (ptr[strlen(ptr) - 1] == long_str) {
long_str = 0;
ptr[strlen(ptr) - 1] = '\0';
}
++argc;
}
}
argv[argc] = NULL;
optind = 0;
while (1) {
option_index = 0;
c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 'h':
cmd_xpath_help();
ret = 0;
goto cleanup;
case 'e':
expr = optarg;
break;
case 't':
if (!strcmp(optarg, "auto")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK;
} else if (!strcmp(optarg, "config")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG;
} else if (!strcmp(optarg, "get")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET;
} else if (!strcmp(optarg, "getconfig")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG;
} else if (!strcmp(optarg, "edit")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT;
} else if (!strcmp(optarg, "rpc")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC;
} else if (!strcmp(optarg, "rpcreply")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY;
} else if (!strcmp(optarg, "notif")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF;
} else if (!strcmp(optarg, "yangdata")) {
options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE;
} else {
fprintf(stderr, "Invalid parser option \"%s\".\n", optarg);
cmd_data_help();
goto cleanup;
}
break;
case 'x':
val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG);
if (!val_tree) {
fprintf(stderr, "Failed to parse the additional data tree for validation.\n");
goto cleanup;
}
break;
case '?':
fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
goto cleanup;
}
}
if (optind == argc) {
fprintf(stderr, "Missing the file with data.\n");
goto cleanup;
}
if (!expr) {
fprintf(stderr, "Missing the XPath expression.\n");
goto cleanup;
}
if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) {
goto cleanup;
}
if (!(set = lyd_find_path(data, expr))) {
goto cleanup;
}
/* print result */
printf("Result:\n");
if (!set->number) {
printf("\tEmpty\n");
} else {
for (i = 0; i < set->number; ++i) {
node = set->set.d[i];
switch (node->schema->nodetype) {
case LYS_CONTAINER:
printf("\tContainer ");
break;
case LYS_LEAF:
printf("\tLeaf ");
break;
case LYS_LEAFLIST:
printf("\tLeaflist ");
break;
case LYS_LIST:
printf("\tList ");
break;
case LYS_ANYXML:
printf("\tAnyxml ");
break;
case LYS_ANYDATA:
printf("\tAnydata ");
break;
default:
printf("\tUnknown ");
break;
}
printf("\"%s\"", node->schema->name);
if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str);
} else if (node->schema->nodetype == LYS_LIST) {
key = (struct lyd_node_leaf_list *)node->child;
printf(" (");
for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) {
if (j) {
printf(" ");
}
printf("\"%s\": %s", key->schema->name, key->value_str);
key = (struct lyd_node_leaf_list *)key->next;
}
printf(")");
}
printf("\n");
}
}
printf("\n");
ly_set_free(set);
ret = 0;
cleanup:
free(*argv);
free(argv);
lyd_free_withsiblings(data);
return ret;
}
int
print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat)
{
struct lyd_node *ylib;
uint32_t idx = 0, has_modules = 0;
uint8_t u;