Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file commands.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool commands |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 10e89e7 | 2016-03-01 16:00:27 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 13 | */ |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 14 | |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <stdio.h> |
| 17 | #include <errno.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 18 | #include <assert.h> |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <getopt.h> |
| 23 | |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 24 | #include "commands.h" |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 25 | #include "../../src/libyang.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 26 | #include "../../src/tree_schema.h" |
| 27 | #include "../../src/tree_data.h" |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 28 | #include "../../src/parser.h" |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 29 | #include "../../src/xpath.h" |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 30 | |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 31 | COMMAND commands[]; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 32 | extern int done; |
| 33 | extern struct ly_ctx *ctx; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 34 | |
| 35 | void |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 36 | cmd_add_help(void) |
| 37 | { |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 38 | printf("add <path-to-model> [<other-models> ...]\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
| 42 | cmd_print_help(void) |
| 43 | { |
Michal Vasko | 5d51c0a | 2016-02-05 14:29:54 +0100 | [diff] [blame] | 44 | printf("print [-f (yang | yin | tree | info)] [-t <info-target-node>] [-o <output-file>] <model-name>[@<revision>]\n\n"); |
Michal Vasko | 3c64ad0 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 45 | printf("\tinfo-target-node: <absolute-schema-node> | typedef[<absolute-schema-nodeid]/<typedef-name> |\n"); |
Michal Vasko | 035f523 | 2015-07-15 12:26:52 +0200 | [diff] [blame] | 46 | printf("\t | identity/<identity-name> | feature/<feature-name> |\n"); |
Michal Vasko | d04dbea | 2015-08-06 15:10:19 +0200 | [diff] [blame] | 47 | printf("\t | grouping/<grouping-name>(<absolute-schema-nodeid>) |\n"); |
Michal Vasko | 035f523 | 2015-07-15 12:26:52 +0200 | [diff] [blame] | 48 | printf("\t | type/<absolute-schema-node-leaf-or-leaflist>\n"); |
Michal Vasko | d04dbea | 2015-08-06 15:10:19 +0200 | [diff] [blame] | 49 | printf("\n"); |
| 50 | printf("\tabsolute-schema-nodeid: ( /(<import-prefix>:)<node-identifier> )+\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 54 | cmd_data_help(void) |
| 55 | { |
Michal Vasko | 78d8a3a | 2016-09-21 11:25:05 +0200 | [diff] [blame] | 56 | printf("data [-(-s)trict] [-x OPTION] [-d DEFAULTS] [-o <output-file>] [-f (xml | json)] [-t <additional-tree-file-name>]\n"); |
| 57 | printf(" <data-file-name> [<JSON-rpc/action-schema-nodeid>]\n"); |
Radek Krejci | 9a71612 | 2015-12-15 15:15:32 +0100 | [diff] [blame] | 58 | printf("Accepted OPTIONs:\n"); |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 59 | printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n"); |
| 60 | printf("\t this option is applicable only in case of XML input data.\n"); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 61 | printf("\tconfig - LYD_OPT_CONFIG\n"); |
Radek Krejci | 9a71612 | 2015-12-15 15:15:32 +0100 | [diff] [blame] | 62 | printf("\tget - LYD_OPT_GET\n"); |
| 63 | printf("\tgetconfig - LYD_OPT_GETCONFIG\n"); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 64 | printf("\tedit - LYD_OPT_EDIT\n"); |
| 65 | printf("\trpc - LYD_OPT_RPC\n"); |
Michal Vasko | 2641417 | 2016-09-21 09:42:41 +0200 | [diff] [blame] | 66 | printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n"); |
Radek Krejci | 92ece00 | 2016-04-04 15:45:05 +0200 | [diff] [blame] | 67 | printf("\tnotif - LYD_OPT_NOTIF\n\n"); |
Michal Vasko | 104f58c | 2016-04-11 11:04:13 +0200 | [diff] [blame] | 68 | printf("Accepted DEFAULTS:\n"); |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 69 | printf("\tall - add missing default nodes\n"); |
Michal Vasko | 104f58c | 2016-04-11 11:04:13 +0200 | [diff] [blame] | 70 | printf("\tall-tagged - add missing default nodes and mark all the default nodes with the attribute.\n"); |
| 71 | printf("\ttrim - remove all nodes with a default value\n"); |
Michal Vasko | 78d8a3a | 2016-09-21 11:25:05 +0200 | [diff] [blame] | 72 | printf("\timplicit-tagged - add missing nodes and mark them with the attribute\n\n"); |
| 73 | printf("Option -t:\n"); |
| 74 | printf("\tIf RPC/action/notification/RPC reply (for OPTIONs 'rpc', 'rpcreply', and 'notif') includes\n"); |
| 75 | printf("\tan XPath expression (when/must) that needs access to the configuration data, you can provide\n"); |
| 76 | printf("\tthem in a file, which will be parsed as 'config'.\n"); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 80 | cmd_xpath_help(void) |
| 81 | { |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 82 | printf("xpath [-x OPTION] [-t <additional-tree-file-name>] -e <XPath-expression>\n" |
| 83 | " <XML-data-file-name> [<JSON-rpc/action-schema-nodeid>]\n"); |
| 84 | printf("Accepted OPTIONs:\n"); |
| 85 | printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n"); |
| 86 | printf("\t this option is applicable only in case of XML input data.\n"); |
| 87 | printf("\tconfig - LYD_OPT_CONFIG\n"); |
| 88 | printf("\tget - LYD_OPT_GET\n"); |
| 89 | printf("\tgetconfig - LYD_OPT_GETCONFIG\n"); |
| 90 | printf("\tedit - LYD_OPT_EDIT\n"); |
| 91 | printf("\trpc - LYD_OPT_RPC\n"); |
| 92 | printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n"); |
| 93 | printf("\tnotif - LYD_OPT_NOTIF\n\n"); |
| 94 | printf("Option -t:\n"); |
| 95 | printf("\tIf RPC/action/notification/RPC reply (for OPTIONs 'rpc', 'rpcreply', and 'notif') includes\n"); |
| 96 | printf("\tan XPath expression (when/must) that needs access to the configuration data, you can provide\n"); |
| 97 | printf("\tthem in a file, which will be parsed as 'config'.\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | cmd_list_help(void) |
| 102 | { |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 103 | printf("list [-f (xml | json)]\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 107 | cmd_feature_help(void) |
| 108 | { |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 109 | printf("feature [ -(-e)nable | -(-d)isable (* | <feature-name>[,<feature-name> ...]) ] <model-name>[@<revision>]\n"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 113 | cmd_searchpath_help(void) |
| 114 | { |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 115 | printf("searchpath <model-dir-path>\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | cmd_verb_help(void) |
| 120 | { |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 121 | printf("verb (error/0 | warning/1 | verbose/2 | debug/3)\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Radek Krejci | 797590b | 2016-10-10 16:23:04 +0200 | [diff] [blame] | 124 | LYS_INFORMAT |
Radek Krejci | dbda8e4 | 2016-10-20 13:24:06 +0200 | [diff] [blame] | 125 | get_schema_format(const char *path) |
Radek Krejci | 797590b | 2016-10-10 16:23:04 +0200 | [diff] [blame] | 126 | { |
| 127 | char *ptr; |
| 128 | |
| 129 | if ((ptr = strrchr(path, '.')) != NULL) { |
| 130 | ++ptr; |
| 131 | if (!strcmp(ptr, "yin")) { |
| 132 | return LYS_IN_YIN; |
| 133 | } else if (!strcmp(ptr, "yang")) { |
| 134 | return LYS_IN_YANG; |
| 135 | } else { |
| 136 | fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr); |
Radek Krejci | 797590b | 2016-10-10 16:23:04 +0200 | [diff] [blame] | 137 | return LYS_IN_UNKNOWN; |
| 138 | } |
| 139 | } else { |
| 140 | fprintf(stdout, "Input file \"%s\" without extension - unknown format.\n", path); |
| 141 | return LYS_IN_UNKNOWN; |
| 142 | } |
| 143 | } |
| 144 | |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 145 | int |
| 146 | cmd_add(const char *arg) |
| 147 | { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 148 | int path_len; |
Radek Krejci | 797590b | 2016-10-10 16:23:04 +0200 | [diff] [blame] | 149 | char *path; |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 150 | const char *arg_ptr; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 151 | const struct lys_module *model; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 152 | LYS_INFORMAT format; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 153 | |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 154 | if (strlen(arg) < 5) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 155 | cmd_add_help(); |
| 156 | return 1; |
| 157 | } |
| 158 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 159 | arg_ptr = arg + strlen("add "); |
| 160 | while (arg_ptr[0] == ' ') { |
| 161 | ++arg_ptr; |
| 162 | } |
| 163 | if (strchr(arg_ptr, ' ')) { |
| 164 | path_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 165 | } else { |
| 166 | path_len = strlen(arg_ptr); |
| 167 | } |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 168 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 169 | path = strndup(arg_ptr, path_len); |
| 170 | |
| 171 | while (path) { |
Radek Krejci | 797590b | 2016-10-10 16:23:04 +0200 | [diff] [blame] | 172 | format = get_schema_format(path); |
| 173 | if (format == LYS_IN_UNKNOWN) { |
| 174 | free(path); |
| 175 | return 1; |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 178 | model = lys_parse_path(ctx, path, format); |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 179 | free(path); |
Radek Krejci | 4041a2d | 2015-07-02 09:16:42 +0200 | [diff] [blame] | 180 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 181 | if (!model) { |
| 182 | /* libyang printed the error messages */ |
| 183 | return 1; |
| 184 | } |
Radek Krejci | 4041a2d | 2015-07-02 09:16:42 +0200 | [diff] [blame] | 185 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 186 | /* next model */ |
| 187 | arg_ptr += path_len; |
| 188 | while (arg_ptr[0] == ' ') { |
| 189 | ++arg_ptr; |
| 190 | } |
| 191 | if (strchr(arg_ptr, ' ')) { |
| 192 | path_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 193 | } else { |
| 194 | path_len = strlen(arg_ptr); |
| 195 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 196 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame] | 197 | if (path_len) { |
| 198 | path = strndup(arg_ptr, path_len); |
| 199 | } else { |
| 200 | path = NULL; |
| 201 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int |
| 208 | cmd_print(const char *arg) |
| 209 | { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 210 | int c, argc, option_index, ret = 1; |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 211 | char **argv = NULL, *ptr, *target_node = NULL, *model_name, *revision; |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 212 | const char *out_path = NULL; |
| 213 | const struct lys_module *module; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 214 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 215 | FILE *output = stdout; |
| 216 | static struct option long_options[] = { |
| 217 | {"help", no_argument, 0, 'h'}, |
| 218 | {"format", required_argument, 0, 'f'}, |
| 219 | {"output", required_argument, 0, 'o'}, |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 220 | {"target-node", required_argument, 0, 't'}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 221 | {NULL, 0, 0, 0} |
| 222 | }; |
| 223 | |
| 224 | argc = 1; |
| 225 | argv = malloc(2*sizeof *argv); |
| 226 | *argv = strdup(arg); |
| 227 | ptr = strtok(*argv, " "); |
| 228 | while ((ptr = strtok(NULL, " "))) { |
| 229 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 230 | argv[argc++] = ptr; |
| 231 | } |
| 232 | argv[argc] = NULL; |
| 233 | |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 234 | optind = 0; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 235 | while (1) { |
Michal Vasko | 1074ce9 | 2015-07-03 16:16:31 +0200 | [diff] [blame] | 236 | option_index = 0; |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 237 | c = getopt_long(argc, argv, "hf:o:t:", long_options, &option_index); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 238 | if (c == -1) { |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | switch (c) { |
| 243 | case 'h': |
| 244 | cmd_print_help(); |
| 245 | ret = 0; |
| 246 | goto cleanup; |
| 247 | case 'f': |
| 248 | if (!strcmp(optarg, "yang")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 249 | format = LYS_OUT_YANG; |
Michal Vasko | 5d51c0a | 2016-02-05 14:29:54 +0100 | [diff] [blame] | 250 | } else if (!strcmp(optarg, "yin")) { |
| 251 | format = LYS_OUT_YIN; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 252 | } else if (!strcmp(optarg, "tree")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 253 | format = LYS_OUT_TREE; |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 254 | } else if (!strcmp(optarg, "info")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 255 | format = LYS_OUT_INFO; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 256 | } else { |
| 257 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 258 | goto cleanup; |
| 259 | } |
| 260 | break; |
| 261 | case 'o': |
| 262 | if (out_path) { |
| 263 | fprintf(stderr, "Output specified twice.\n"); |
| 264 | goto cleanup; |
| 265 | } |
| 266 | out_path = optarg; |
| 267 | break; |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 268 | case 't': |
| 269 | target_node = optarg; |
| 270 | break; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 271 | case '?': |
| 272 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 273 | goto cleanup; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* file name */ |
| 278 | if (optind == argc) { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 279 | fprintf(stderr, "Missing the module name.\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 280 | goto cleanup; |
| 281 | } |
Michal Vasko | cf0a41c | 2015-07-07 11:43:10 +0200 | [diff] [blame] | 282 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 283 | /* module, revision */ |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 284 | model_name = argv[optind]; |
| 285 | revision = NULL; |
| 286 | if (strchr(model_name, '@')) { |
| 287 | revision = strchr(model_name, '@'); |
| 288 | revision[0] = '\0'; |
| 289 | ++revision; |
| 290 | } |
| 291 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 292 | module = ly_ctx_get_module(ctx, model_name, revision); |
| 293 | if (!module) { |
| 294 | /* not a module, try to find it as a submodule */ |
| 295 | module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision); |
Michal Vasko | cf0a41c | 2015-07-07 11:43:10 +0200 | [diff] [blame] | 296 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 297 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 298 | if (!module) { |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 299 | if (revision) { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 300 | fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision); |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 301 | } else { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 302 | fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name); |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 303 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 304 | goto cleanup; |
| 305 | } |
| 306 | |
| 307 | if (out_path) { |
| 308 | output = fopen(out_path, "w"); |
| 309 | if (!output) { |
| 310 | fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno)); |
| 311 | goto cleanup; |
| 312 | } |
| 313 | } |
| 314 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 315 | ret = lys_print_file(output, module, format, target_node); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 316 | |
| 317 | cleanup: |
| 318 | free(*argv); |
| 319 | free(argv); |
| 320 | |
Radek Krejci | 94f0583 | 2015-06-19 09:58:53 +0200 | [diff] [blame] | 321 | if (output && (output != stdout)) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 322 | fclose(output); |
| 323 | } |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 328 | static int |
| 329 | parse_data(const char *filepath, int options, struct lyd_node *val_tree, const char *act_nodeid, |
| 330 | struct lyd_node **result) |
| 331 | { |
| 332 | size_t len; |
| 333 | LYD_FORMAT informat = LYD_UNKNOWN; |
| 334 | struct lyxml_elem *xml = NULL; |
| 335 | const struct lys_node *rpc_act = NULL; |
| 336 | struct lyd_node *data = NULL, *root, *next, *iter; |
| 337 | |
| 338 | /* detect input format according to file suffix */ |
| 339 | len = strlen(filepath); |
| 340 | if (len >= 5 && !strcmp(&filepath[len - 4], ".xml")) { |
| 341 | informat = LYD_XML; |
| 342 | } else if (len >= 6 && !strcmp(&filepath[len - 5], ".json")) { |
| 343 | informat = LYD_JSON; |
| 344 | } else { |
| 345 | fprintf(stderr, "Unable to resolve format of the input file, please add \".xml\" or \".json\" suffix.\n"); |
| 346 | return EXIT_FAILURE; |
| 347 | } |
| 348 | |
| 349 | ly_errno = LY_SUCCESS; |
| 350 | |
| 351 | if ((options & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) { |
| 352 | /* automatically detect data type from the data top level */ |
| 353 | if (informat != LYD_XML) { |
| 354 | fprintf(stderr, "Only XML data can be automatically explored.\n"); |
| 355 | return EXIT_FAILURE; |
| 356 | } |
| 357 | |
| 358 | xml = lyxml_parse_path(ctx, filepath, 0); |
| 359 | if (!xml) { |
| 360 | fprintf(stderr, "Failed to parse XML data for automatic type detection.\n"); |
| 361 | return EXIT_FAILURE; |
| 362 | } |
| 363 | |
| 364 | /* NOTE: namespace is ignored to simplify usage of this feature */ |
| 365 | |
| 366 | if (!strcmp(xml->name, "data")) { |
| 367 | fprintf(stdout, "Parsing %s as complete datastore.\n", filepath); |
| 368 | options = (options & ~LYD_OPT_TYPEMASK); |
| 369 | } else if (!strcmp(xml->name, "config")) { |
| 370 | fprintf(stdout, "Parsing %s as config data.\n", filepath); |
| 371 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 372 | } else if (!strcmp(xml->name, "get-reply")) { |
| 373 | fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath); |
| 374 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 375 | } else if (!strcmp(xml->name, "get-config-reply")) { |
| 376 | fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath); |
| 377 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 378 | } else if (!strcmp(xml->name, "edit-config")) { |
| 379 | fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath); |
| 380 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 381 | } else if (!strcmp(xml->name, "rpc")) { |
| 382 | fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath); |
| 383 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 384 | } else if (!strcmp(xml->name, "rpc-reply")) { |
| 385 | if (!act_nodeid) { |
| 386 | fprintf(stderr, "RPC reply data require additional argument (JSON schema nodeid of the RPC/action).\n"); |
| 387 | lyxml_free(ctx, xml); |
| 388 | return EXIT_FAILURE; |
| 389 | } |
| 390 | fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath); |
| 391 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY; |
| 392 | rpc_act = ly_ctx_get_node(ctx, NULL, act_nodeid); |
| 393 | if (!rpc_act || !(rpc_act->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 394 | fprintf(stderr, "Invalid JSON schema nodeid.\n"); |
| 395 | lyxml_free(ctx, xml); |
| 396 | return EXIT_FAILURE; |
| 397 | } |
| 398 | } else if (!strcmp(xml->name, "notification")) { |
| 399 | fprintf(stdout, "Parsing %s as <notification> data.\n", filepath); |
| 400 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 401 | } else { |
| 402 | fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n"); |
| 403 | lyxml_free(ctx, xml); |
| 404 | return EXIT_FAILURE; |
| 405 | } |
| 406 | |
| 407 | if (options & LYD_OPT_RPCREPLY) { |
| 408 | data = lyd_parse_xml(ctx, &xml->child, options, rpc_act, val_tree); |
| 409 | } else if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) { |
| 410 | data = lyd_parse_xml(ctx, &xml->child, options, val_tree); |
| 411 | } else { |
| 412 | if ((options & LYD_OPT_TYPEMASK) == LYD_OPT_DATA && !(options & LYD_OPT_STRICT )) { |
| 413 | /* we have to include status data from ietf-yang-library which is part of the context, |
| 414 | * so we have to postpone validation after merging input data with ly_ctx_info() */ |
| 415 | options |= LYD_OPT_TRUSTED; |
| 416 | } |
| 417 | data = lyd_parse_xml(ctx, &xml->child, options); |
| 418 | } |
| 419 | lyxml_free(ctx, xml); |
| 420 | } else { |
| 421 | if (options & LYD_OPT_RPCREPLY) { |
| 422 | if (act_nodeid) { |
| 423 | fprintf(stderr, "RPC reply data require additional argument (schema nodeid of the RPC/action).\n"); |
| 424 | return EXIT_FAILURE; |
| 425 | } |
| 426 | rpc_act = ly_ctx_get_node(ctx, NULL, act_nodeid); |
| 427 | if (!rpc_act || !(rpc_act->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 428 | fprintf(stderr, "Invalid JSON schema nodeid.\n"); |
| 429 | return EXIT_FAILURE; |
| 430 | } |
| 431 | data = lyd_parse_path(ctx, filepath, informat, options, rpc_act, val_tree); |
| 432 | } else if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) { |
| 433 | data = lyd_parse_path(ctx, filepath, informat, options, val_tree); |
| 434 | } else { |
| 435 | if ((options & LYD_OPT_TYPEMASK) == LYD_OPT_DATA && !(options & LYD_OPT_STRICT )) { |
| 436 | /* we have to include status data from ietf-yang-library which is part of the context, |
| 437 | * so we have to postpone validation after merging input data with ly_ctx_info() */ |
| 438 | options |= LYD_OPT_TRUSTED; |
| 439 | } |
| 440 | data = lyd_parse_path(ctx, filepath, informat, options); |
| 441 | } |
| 442 | } |
| 443 | if (ly_errno) { |
| 444 | fprintf(stderr, "Failed to parse data.\n"); |
| 445 | lyd_free_withsiblings(data); |
| 446 | return EXIT_FAILURE; |
| 447 | } |
| 448 | |
| 449 | if (options & LYD_OPT_TRUSTED) { |
| 450 | /* postponed validation in case of LYD_OPT_DATA */ |
| 451 | /* remove the trusted flag */ |
| 452 | options &= ~LYD_OPT_TRUSTED; |
| 453 | |
| 454 | /* merge with ietf-yang-library */ |
| 455 | root = ly_ctx_info(ctx); |
| 456 | if (lyd_merge(root, data, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) { |
| 457 | fprintf(stderr, "Merging input data with ietf-yang-library failed.\n"); |
| 458 | lyd_free_withsiblings(root); |
| 459 | lyd_free_withsiblings(data); |
| 460 | return EXIT_FAILURE; |
| 461 | } |
| 462 | data = root; |
| 463 | |
| 464 | /* invalidate all data - do not believe to the source */ |
| 465 | LY_TREE_FOR(data, root) { |
| 466 | LY_TREE_DFS_BEGIN(root, next, iter) { |
| 467 | iter->validity = LYD_VAL_NOT; |
| 468 | LY_TREE_DFS_END(root, next, iter) |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* validate the result */ |
| 473 | if (lyd_validate(&data, options, NULL)) { |
| 474 | fprintf(stderr, "Failed to parse data.\n"); |
| 475 | lyd_free_withsiblings(data); |
| 476 | return EXIT_FAILURE; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | *result = data; |
| 481 | return EXIT_SUCCESS; |
| 482 | } |
| 483 | |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 484 | int |
| 485 | cmd_data(const char *arg) |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 486 | { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 487 | int c, argc, option_index, ret = 1; |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 488 | int options = 0, printopt = 0; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 489 | char **argv = NULL, *ptr; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 490 | const char *out_path = NULL; |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 491 | struct lyd_node *data = NULL, *val_tree = NULL; |
| 492 | LYD_FORMAT outformat = LYD_UNKNOWN; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 493 | FILE *output = stdout; |
| 494 | static struct option long_options[] = { |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 495 | {"defaults", required_argument, 0, 'd'}, |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 496 | {"help", no_argument, 0, 'h'}, |
| 497 | {"format", required_argument, 0, 'f'}, |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 498 | {"option", required_argument, 0, 'x'}, |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 499 | {"output", required_argument, 0, 'o'}, |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 500 | {"strict", no_argument, 0, 's'}, |
Michal Vasko | 78d8a3a | 2016-09-21 11:25:05 +0200 | [diff] [blame] | 501 | {"validation-tree", required_argument, 0, 't'}, |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 502 | {NULL, 0, 0, 0} |
| 503 | }; |
| 504 | |
| 505 | argc = 1; |
| 506 | argv = malloc(2*sizeof *argv); |
| 507 | *argv = strdup(arg); |
| 508 | ptr = strtok(*argv, " "); |
| 509 | while ((ptr = strtok(NULL, " "))) { |
| 510 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 511 | argv[argc++] = ptr; |
| 512 | } |
| 513 | argv[argc] = NULL; |
| 514 | |
| 515 | optind = 0; |
| 516 | while (1) { |
| 517 | option_index = 0; |
Michal Vasko | 78d8a3a | 2016-09-21 11:25:05 +0200 | [diff] [blame] | 518 | c = getopt_long(argc, argv, "d:hf:o:sx:t:", long_options, &option_index); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 519 | if (c == -1) { |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | switch (c) { |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 524 | case 'd': |
| 525 | if (!strcmp(optarg, "all")) { |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 526 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL; |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 527 | } else if (!strcmp(optarg, "all-tagged")) { |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 528 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG; |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 529 | } else if (!strcmp(optarg, "trim")) { |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 530 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM; |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 531 | } else if (!strcmp(optarg, "implicit-tagged")) { |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 532 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG; |
Radek Krejci | 4bfa664 | 2016-03-23 15:57:45 +0100 | [diff] [blame] | 533 | } |
| 534 | break; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 535 | case 'h': |
| 536 | cmd_data_help(); |
| 537 | ret = 0; |
| 538 | goto cleanup; |
| 539 | case 'f': |
| 540 | if (!strcmp(optarg, "xml")) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 541 | outformat = LYD_XML; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 542 | } else if (!strcmp(optarg, "json")) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 543 | outformat = LYD_JSON; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 544 | } else { |
| 545 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 546 | goto cleanup; |
| 547 | } |
| 548 | break; |
| 549 | case 'o': |
| 550 | if (out_path) { |
| 551 | fprintf(stderr, "Output specified twice.\n"); |
| 552 | goto cleanup; |
| 553 | } |
| 554 | out_path = optarg; |
| 555 | break; |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 556 | case 's': |
| 557 | options |= LYD_OPT_STRICT; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 558 | options |= LYD_OPT_OBSOLETE; |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 559 | break; |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 560 | case 'x': |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 561 | if (!strcmp(optarg, "auto")) { |
| 562 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK; |
| 563 | } else if (!strcmp(optarg, "config")) { |
| 564 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 565 | } else if (!strcmp(optarg, "get")) { |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 566 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 567 | } else if (!strcmp(optarg, "getconfig")) { |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 568 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 569 | } else if (!strcmp(optarg, "edit")) { |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 570 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 571 | } else if (!strcmp(optarg, "rpc")) { |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 572 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 573 | } else if (!strcmp(optarg, "rpcreply")) { |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 574 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY; |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 575 | } else if (!strcmp(optarg, "notif")) { |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 576 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
Radek Krejci | 83b5ae5 | 2016-03-11 11:17:26 +0100 | [diff] [blame] | 577 | } else { |
| 578 | fprintf(stderr, "Invalid parser option \"%s\".\n", optarg); |
| 579 | cmd_data_help(); |
| 580 | goto cleanup; |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 581 | } |
| 582 | break; |
Michal Vasko | 78d8a3a | 2016-09-21 11:25:05 +0200 | [diff] [blame] | 583 | case 't': |
| 584 | val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG); |
| 585 | if (!val_tree) { |
| 586 | fprintf(stderr, "Failed to parse the additional data tree for validation.\n"); |
| 587 | goto cleanup; |
| 588 | } |
| 589 | break; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 590 | case '?': |
| 591 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 592 | goto cleanup; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /* file name */ |
| 597 | if (optind == argc) { |
| 598 | fprintf(stderr, "Missing the data file name.\n"); |
| 599 | goto cleanup; |
| 600 | } |
| 601 | |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 602 | if (parse_data(argv[optind], options, val_tree, argv[optind + 1], &data)) { |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 603 | goto cleanup; |
| 604 | } |
| 605 | |
| 606 | if (out_path) { |
| 607 | output = fopen(out_path, "w"); |
| 608 | if (!output) { |
| 609 | fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno)); |
| 610 | goto cleanup; |
| 611 | } |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 614 | if (outformat != LYD_UNKNOWN) { |
Michal Vasko | 2641417 | 2016-09-21 09:42:41 +0200 | [diff] [blame] | 615 | if (options & LYD_OPT_RPCREPLY) { |
| 616 | lyd_print_file(output, data->child, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt); |
| 617 | } else { |
| 618 | lyd_print_file(output, data, outformat, LYP_WITHSIBLINGS | LYP_FORMAT | printopt); |
| 619 | } |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 622 | ret = 0; |
| 623 | |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 624 | cleanup: |
| 625 | free(*argv); |
| 626 | free(argv); |
| 627 | |
| 628 | if (output && (output != stdout)) { |
| 629 | fclose(output); |
| 630 | } |
| 631 | |
Michal Vasko | 78d8a3a | 2016-09-21 11:25:05 +0200 | [diff] [blame] | 632 | lyd_free_withsiblings(val_tree); |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 633 | lyd_free_withsiblings(data); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 634 | |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | int |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 639 | cmd_xpath(const char *arg) |
| 640 | { |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 641 | int c, argc, option_index, ret = 1, long_str; |
| 642 | char **argv = NULL, *ptr, *expr = NULL; |
| 643 | unsigned int i, j; |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 644 | int options = 0; |
| 645 | struct lyd_node *data = NULL, *node, *val_tree = NULL; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 646 | struct lyd_node_leaf_list *key; |
| 647 | struct ly_set *set; |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 648 | static struct option long_options[] = { |
| 649 | {"help", no_argument, 0, 'h'}, |
| 650 | {"expr", required_argument, 0, 'e'}, |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 651 | {NULL, 0, 0, 0} |
| 652 | }; |
| 653 | |
| 654 | long_str = 0; |
| 655 | argc = 1; |
| 656 | argv = malloc(2 * sizeof *argv); |
| 657 | *argv = strdup(arg); |
| 658 | ptr = strtok(*argv, " "); |
| 659 | while ((ptr = strtok(NULL, " "))) { |
| 660 | if (long_str) { |
| 661 | ptr[-1] = ' '; |
| 662 | if (ptr[strlen(ptr) - 1] == long_str) { |
| 663 | long_str = 0; |
| 664 | ptr[strlen(ptr) - 1] = '\0'; |
| 665 | } |
| 666 | } else { |
| 667 | argv = realloc(argv, (argc + 2) * sizeof *argv); |
| 668 | argv[argc] = ptr; |
| 669 | if (ptr[0] == '"') { |
| 670 | long_str = '"'; |
| 671 | ++argv[argc]; |
| 672 | } |
| 673 | if (ptr[0] == '\'') { |
| 674 | long_str = '\''; |
| 675 | ++argv[argc]; |
| 676 | } |
| 677 | if (ptr[strlen(ptr) - 1] == long_str) { |
| 678 | long_str = 0; |
| 679 | ptr[strlen(ptr) - 1] = '\0'; |
| 680 | } |
| 681 | ++argc; |
| 682 | } |
| 683 | } |
| 684 | argv[argc] = NULL; |
| 685 | |
| 686 | optind = 0; |
| 687 | while (1) { |
| 688 | option_index = 0; |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 689 | c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index); |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 690 | if (c == -1) { |
| 691 | break; |
| 692 | } |
| 693 | |
| 694 | switch (c) { |
| 695 | case 'h': |
| 696 | cmd_xpath_help(); |
| 697 | ret = 0; |
| 698 | goto cleanup; |
| 699 | case 'e': |
| 700 | expr = optarg; |
| 701 | break; |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 702 | case 'x': |
| 703 | if (!strcmp(optarg, "auto")) { |
| 704 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK; |
| 705 | } else if (!strcmp(optarg, "config")) { |
| 706 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 707 | } else if (!strcmp(optarg, "get")) { |
| 708 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 709 | } else if (!strcmp(optarg, "getconfig")) { |
| 710 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 711 | } else if (!strcmp(optarg, "edit")) { |
| 712 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 713 | } else if (!strcmp(optarg, "rpc")) { |
| 714 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 715 | } else if (!strcmp(optarg, "rpcreply")) { |
| 716 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY; |
| 717 | } else if (!strcmp(optarg, "notif")) { |
| 718 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 719 | } else { |
| 720 | fprintf(stderr, "Invalid parser option \"%s\".\n", optarg); |
| 721 | cmd_data_help(); |
| 722 | goto cleanup; |
| 723 | } |
| 724 | break; |
| 725 | case 't': |
| 726 | val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG); |
| 727 | if (!val_tree) { |
| 728 | fprintf(stderr, "Failed to parse the additional data tree for validation.\n"); |
| 729 | goto cleanup; |
| 730 | } |
| 731 | break; |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 732 | case '?': |
| 733 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 734 | goto cleanup; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (optind == argc) { |
| 739 | fprintf(stderr, "Missing the file with data.\n"); |
| 740 | goto cleanup; |
| 741 | } |
| 742 | |
| 743 | if (!expr) { |
| 744 | fprintf(stderr, "Missing the XPath expression.\n"); |
| 745 | goto cleanup; |
| 746 | } |
| 747 | |
Radek Krejci | fad2864 | 2016-10-31 13:06:57 +0100 | [diff] [blame] | 748 | if (parse_data(argv[optind], options, val_tree, argv[optind + 1], &data)) { |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 749 | goto cleanup; |
| 750 | } |
| 751 | |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 752 | if (!(set = lyd_find_xpath(data, expr))) { |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 753 | goto cleanup; |
| 754 | } |
| 755 | |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 756 | /* print result */ |
| 757 | printf("Result:\n"); |
| 758 | if (!set->number) { |
| 759 | printf("\tEmpty\n"); |
| 760 | } else { |
| 761 | for (i = 0; i < set->number; ++i) { |
Radek Krejci | 8f08df1 | 2016-03-21 11:11:30 +0100 | [diff] [blame] | 762 | node = set->set.d[i]; |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 763 | switch (node->schema->nodetype) { |
| 764 | case LYS_CONTAINER: |
| 765 | printf("\tContainer "); |
| 766 | break; |
| 767 | case LYS_LEAF: |
| 768 | printf("\tLeaf "); |
| 769 | break; |
| 770 | case LYS_LEAFLIST: |
| 771 | printf("\tLeaflist "); |
| 772 | break; |
| 773 | case LYS_LIST: |
Michal Vasko | dbb8632 | 2016-02-04 13:53:38 +0100 | [diff] [blame] | 774 | printf("\tList "); |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 775 | break; |
| 776 | case LYS_ANYXML: |
| 777 | printf("\tAnyxml "); |
| 778 | break; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 779 | case LYS_ANYDATA: |
| 780 | printf("\tAnydata "); |
| 781 | break; |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 782 | default: |
| 783 | printf("\tUnknown "); |
| 784 | break; |
| 785 | } |
| 786 | printf("\"%s\"", node->schema->name); |
| 787 | if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 788 | printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str); |
| 789 | } else if (node->schema->nodetype == LYS_LIST) { |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 790 | key = (struct lyd_node_leaf_list *)node->child; |
| 791 | printf(" ("); |
| 792 | for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) { |
| 793 | if (j) { |
| 794 | printf(" "); |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 795 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 796 | printf("\"%s\": %s", key->schema->name, key->value_str); |
| 797 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 798 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 799 | printf(")"); |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 800 | } |
| 801 | printf("\n"); |
| 802 | } |
| 803 | } |
| 804 | printf("\n"); |
| 805 | |
Michal Vasko | 2b67bf7 | 2016-05-05 17:49:36 +0200 | [diff] [blame] | 806 | ly_set_free(set); |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 807 | ret = 0; |
| 808 | |
| 809 | cleanup: |
| 810 | free(*argv); |
| 811 | free(argv); |
| 812 | |
Radek Krejci | 4a2aad8 | 2016-01-13 15:01:07 +0100 | [diff] [blame] | 813 | lyd_free_withsiblings(data); |
Michal Vasko | abbdaa0 | 2015-10-06 15:47:25 +0200 | [diff] [blame] | 814 | |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | int |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 819 | cmd_list(const char *arg) |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 820 | { |
Radek Krejci | fc3692c | 2016-02-25 15:36:13 +0100 | [diff] [blame] | 821 | struct lyd_node *ylib = NULL, *module, *submodule, *node; |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 822 | int has_modules = 0, flag; |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 823 | char **argv = NULL, *ptr; |
| 824 | int c, argc, option_index; |
| 825 | LYD_FORMAT outformat = LYD_UNKNOWN; |
| 826 | static struct option long_options[] = { |
| 827 | {"help", no_argument, 0, 'h'}, |
| 828 | {"format", required_argument, 0, 'f'}, |
| 829 | {NULL, 0, 0, 0} |
| 830 | }; |
| 831 | |
| 832 | argc = 1; |
| 833 | argv = malloc(2*sizeof *argv); |
| 834 | *argv = strdup(arg); |
| 835 | ptr = strtok(*argv, " "); |
| 836 | while ((ptr = strtok(NULL, " "))) { |
| 837 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 838 | argv[argc++] = ptr; |
| 839 | } |
| 840 | argv[argc] = NULL; |
| 841 | |
| 842 | optind = 0; |
| 843 | while (1) { |
| 844 | option_index = 0; |
| 845 | c = getopt_long(argc, argv, "hf:", long_options, &option_index); |
| 846 | if (c == -1) { |
| 847 | break; |
| 848 | } |
| 849 | |
| 850 | switch (c) { |
| 851 | case 'h': |
| 852 | cmd_data_help(); |
| 853 | free(*argv); |
| 854 | free(argv); |
| 855 | return 0; |
| 856 | case 'f': |
| 857 | if (!strcmp(optarg, "xml")) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 858 | outformat = LYD_XML; |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 859 | } else if (!strcmp(optarg, "json")) { |
| 860 | outformat = LYD_JSON; |
| 861 | } else { |
| 862 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
Radek Krejci | fc3692c | 2016-02-25 15:36:13 +0100 | [diff] [blame] | 863 | goto error; |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 864 | } |
| 865 | break; |
| 866 | case '?': |
Radek Krejci | fc3692c | 2016-02-25 15:36:13 +0100 | [diff] [blame] | 867 | /* getopt_long() prints message */ |
| 868 | goto error; |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 869 | } |
| 870 | } |
Radek Krejci | fc3692c | 2016-02-25 15:36:13 +0100 | [diff] [blame] | 871 | if (optind != argc) { |
| 872 | fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]); |
| 873 | error: |
| 874 | free(*argv); |
| 875 | free(argv); |
| 876 | return 1; |
| 877 | } |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 878 | free(*argv); |
| 879 | free(argv); |
| 880 | |
Radek Krejci | 7ab2515 | 2015-08-07 14:48:45 +0200 | [diff] [blame] | 881 | ylib = ly_ctx_info(ctx); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 882 | if (!ylib) { |
Michal Vasko | 7c7023f | 2015-12-11 11:59:54 +0100 | [diff] [blame] | 883 | fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n"); |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 884 | return 1; |
| 885 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 886 | |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 887 | if (outformat != LYD_UNKNOWN) { |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 888 | lyd_print_file(stdout, ylib, outformat, LYP_WITHSIBLINGS | LYP_FORMAT); |
Radek Krejci | dd1c417 | 2016-02-25 15:17:45 +0100 | [diff] [blame] | 889 | lyd_free(ylib); |
Radek Krejci | 83a66b0 | 2016-02-25 15:10:31 +0100 | [diff] [blame] | 890 | return 0; |
| 891 | } |
| 892 | |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 893 | LY_TREE_FOR(ylib->child, node) { |
| 894 | if (!strcmp(node->schema->name, "module-set-id")) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 895 | printf("List of the loaded models (mod-set-id %s):\n", ((struct lyd_node_leaf_list *)node)->value_str); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 896 | break; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 897 | } |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 898 | } |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 899 | assert(node); |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 900 | |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 901 | LY_TREE_FOR(ylib->child, module) { |
| 902 | if (!strcmp(module->schema->name, "module")) { |
| 903 | has_modules = 1; |
| 904 | |
| 905 | /* module print */ |
| 906 | LY_TREE_FOR(module->child, node) { |
| 907 | if (!strcmp(node->schema->name, "name")) { |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 908 | printf("\t%s", ((struct lyd_node_leaf_list *)node)->value_str); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 909 | } else if (!strcmp(node->schema->name, "revision")) { |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 910 | if (((struct lyd_node_leaf_list *)node)->value_str[0] != '\0') { |
| 911 | printf("@%s", ((struct lyd_node_leaf_list *)node)->value_str); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* submodules print */ |
| 917 | LY_TREE_FOR(module->child, submodule) { |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 918 | if (!strcmp(submodule->schema->name, "submodule")) { |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 919 | printf(" ("); |
| 920 | flag = 0; |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 921 | LY_TREE_FOR(submodule, submodule) { |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 922 | if (!strcmp(submodule->schema->name, "submodule")) { |
| 923 | LY_TREE_FOR(submodule->child, node) { |
| 924 | if (!strcmp(node->schema->name, "name")) { |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 925 | printf("%s%s", flag ? "," : "", ((struct lyd_node_leaf_list *)node)->value_str); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 926 | } else if (!strcmp(node->schema->name, "revision")) { |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 927 | if (((struct lyd_node_leaf_list *)node)->value_str[0] != '\0') { |
| 928 | printf("@%s", ((struct lyd_node_leaf_list *)node)->value_str); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | } |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 932 | flag++; |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 933 | } |
| 934 | } |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 935 | printf(")"); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 936 | break; |
| 937 | } |
| 938 | } |
Radek Krejci | 6e05cea | 2015-12-10 16:34:37 +0100 | [diff] [blame] | 939 | printf("\n"); |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
| 943 | if (!has_modules) { |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 944 | printf("\t(none)\n"); |
| 945 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 946 | |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 947 | lyd_free(ylib); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | int |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 952 | cmd_feature(const char *arg) |
| 953 | { |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 954 | int c, i, argc, option_index, ret = 1, task = 0; |
Michal Vasko | b011e6b | 2015-08-06 09:57:46 +0200 | [diff] [blame] | 955 | unsigned int max_len; |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 956 | char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL; |
| 957 | const char **names; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 958 | uint8_t *states; |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 959 | const struct lys_module *module; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 960 | static struct option long_options[] = { |
| 961 | {"help", no_argument, 0, 'h'}, |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 962 | {"enable", required_argument, 0, 'e'}, |
| 963 | {"disable", required_argument, 0, 'd'}, |
| 964 | {NULL, 0, 0, 0} |
| 965 | }; |
| 966 | |
| 967 | argc = 1; |
| 968 | argv = malloc(2*sizeof *argv); |
| 969 | *argv = strdup(arg); |
| 970 | ptr = strtok(*argv, " "); |
| 971 | while ((ptr = strtok(NULL, " "))) { |
| 972 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 973 | argv[argc++] = ptr; |
| 974 | } |
| 975 | argv[argc] = NULL; |
| 976 | |
| 977 | optind = 0; |
| 978 | while (1) { |
| 979 | option_index = 0; |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 980 | c = getopt_long(argc, argv, "he:d:", long_options, &option_index); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 981 | if (c == -1) { |
| 982 | break; |
| 983 | } |
| 984 | |
| 985 | switch (c) { |
| 986 | case 'h': |
| 987 | cmd_feature_help(); |
| 988 | ret = 0; |
| 989 | goto cleanup; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 990 | case 'e': |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 991 | if (task) { |
| 992 | fprintf(stderr, "Only one of enable or disable can be specified.\n"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 993 | goto cleanup; |
| 994 | } |
| 995 | task = 1; |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 996 | feat_names = optarg; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 997 | break; |
| 998 | case 'd': |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 999 | if (task) { |
| 1000 | fprintf(stderr, "Only one of enable, or disable can be specified.\n"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1001 | goto cleanup; |
| 1002 | } |
| 1003 | task = 2; |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 1004 | feat_names = optarg; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1005 | break; |
| 1006 | case '?': |
| 1007 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 1008 | goto cleanup; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1012 | /* module name */ |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1013 | if (optind == argc) { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1014 | fprintf(stderr, "Missing the module name.\n"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1015 | goto cleanup; |
| 1016 | } |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 1017 | |
| 1018 | revision = NULL; |
| 1019 | model_name = argv[optind]; |
| 1020 | if (strchr(model_name, '@')) { |
| 1021 | revision = strchr(model_name, '@'); |
| 1022 | revision[0] = '\0'; |
| 1023 | ++revision; |
| 1024 | } |
| 1025 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1026 | module = ly_ctx_get_module(ctx, model_name, revision); |
| 1027 | if (!module) { |
| 1028 | /* not a module, try to find it as a submodule */ |
| 1029 | module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1030 | } |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1031 | |
| 1032 | if (module == NULL) { |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 1033 | if (revision) { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1034 | fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision); |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 1035 | } else { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1036 | fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name); |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 1037 | } |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1038 | goto cleanup; |
| 1039 | } |
| 1040 | |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 1041 | if (!task) { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1042 | printf("%s features:\n", module->name); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1043 | |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1044 | names = lys_features_list(module, &states); |
Michal Vasko | b011e6b | 2015-08-06 09:57:46 +0200 | [diff] [blame] | 1045 | |
| 1046 | /* get the max len */ |
| 1047 | max_len = 0; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1048 | for (i = 0; names[i]; ++i) { |
Michal Vasko | b011e6b | 2015-08-06 09:57:46 +0200 | [diff] [blame] | 1049 | if (strlen(names[i]) > max_len) { |
| 1050 | max_len = strlen(names[i]); |
| 1051 | } |
| 1052 | } |
| 1053 | for (i = 0; names[i]; ++i) { |
| 1054 | printf("\t%-*s (%s)\n", max_len, names[i], states[i] ? "on" : "off"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1055 | } |
| 1056 | free(names); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 1057 | free(states); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1058 | if (!i) { |
| 1059 | printf("\t(none)\n"); |
| 1060 | } |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 1061 | } else { |
| 1062 | feat_names = strtok(feat_names, ","); |
| 1063 | while (feat_names) { |
Michal Vasko | 462be9a | 2016-04-05 11:24:08 +0200 | [diff] [blame] | 1064 | if (((task == 1) && lys_features_enable(module, feat_names)) |
| 1065 | || ((task == 2) && lys_features_disable(module, feat_names))) { |
Michal Vasko | 8714841 | 2015-08-21 14:29:51 +0200 | [diff] [blame] | 1066 | fprintf(stderr, "Feature \"%s\" not found.\n", feat_names); |
| 1067 | ret = 1; |
| 1068 | } |
| 1069 | feat_names = strtok(NULL, ","); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | cleanup: |
| 1074 | free(*argv); |
| 1075 | free(argv); |
| 1076 | |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | int |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1081 | cmd_searchpath(const char *arg) |
| 1082 | { |
| 1083 | const char *path; |
| 1084 | struct stat st; |
| 1085 | |
| 1086 | if (strchr(arg, ' ') == NULL) { |
| 1087 | fprintf(stderr, "Missing the search path.\n"); |
| 1088 | return 1; |
| 1089 | } |
| 1090 | path = strchr(arg, ' ')+1; |
| 1091 | |
| 1092 | if (!strcmp(path, "-h") || !strcmp(path, "--help")) { |
| 1093 | cmd_searchpath_help(); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | if (stat(path, &st) == -1) { |
| 1098 | fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno)); |
| 1099 | return 1; |
| 1100 | } |
| 1101 | if (!S_ISDIR(st.st_mode)) { |
| 1102 | fprintf(stderr, "\"%s\" is not a directory.\n", path); |
| 1103 | return 1; |
| 1104 | } |
| 1105 | |
Radek Krejci | 0cb4784 | 2015-12-09 15:24:32 +0100 | [diff] [blame] | 1106 | ly_ctx_set_searchdir(ctx, path); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1107 | |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1110 | |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1111 | int |
| 1112 | cmd_clear(const char *UNUSED(arg)) |
| 1113 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1114 | ly_ctx_destroy(ctx, NULL); |
Radek Krejci | 0cb4784 | 2015-12-09 15:24:32 +0100 | [diff] [blame] | 1115 | ctx = ly_ctx_new(NULL); |
Michal Vasko | 5f998eb | 2015-08-21 14:05:58 +0200 | [diff] [blame] | 1116 | if (!ctx) { |
| 1117 | fprintf(stderr, "Failed to create context.\n"); |
| 1118 | return 1; |
| 1119 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | int |
| 1124 | cmd_verb(const char *arg) |
| 1125 | { |
| 1126 | const char *verb; |
Radek Krejci | 7408bf1 | 2015-07-20 18:15:51 +0200 | [diff] [blame] | 1127 | if (strlen(arg) < 5) { |
| 1128 | cmd_verb_help(); |
| 1129 | return 1; |
| 1130 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1131 | |
| 1132 | verb = arg + 5; |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 1133 | if (!strcmp(verb, "error") || !strcmp(verb, "0")) { |
Radek Krejci | 2abfdfe | 2016-07-19 11:05:06 +0200 | [diff] [blame] | 1134 | ly_verb(LY_LLERR); |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 1135 | } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) { |
Radek Krejci | 2abfdfe | 2016-07-19 11:05:06 +0200 | [diff] [blame] | 1136 | ly_verb(LY_LLWRN); |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 1137 | } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) { |
Radek Krejci | 2abfdfe | 2016-07-19 11:05:06 +0200 | [diff] [blame] | 1138 | ly_verb(LY_LLVRB); |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 1139 | } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) { |
Radek Krejci | 2abfdfe | 2016-07-19 11:05:06 +0200 | [diff] [blame] | 1140 | ly_verb(LY_LLDBG); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1141 | } else { |
Radek Krejci | 7408bf1 | 2015-07-20 18:15:51 +0200 | [diff] [blame] | 1142 | fprintf(stderr, "Unknown verbosity \"%s\"\n", verb); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1143 | return 1; |
| 1144 | } |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | int |
| 1150 | cmd_quit(const char *UNUSED(arg)) |
| 1151 | { |
| 1152 | done = 1; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | int |
| 1157 | cmd_help(const char *arg) |
| 1158 | { |
| 1159 | int i; |
Radek Krejci | ec797a3 | 2016-02-05 16:52:12 +0100 | [diff] [blame] | 1160 | char *args = strdup(arg); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1161 | char *cmd = NULL; |
| 1162 | |
| 1163 | strtok(args, " "); |
| 1164 | if ((cmd = strtok(NULL, " ")) == NULL) { |
| 1165 | |
| 1166 | generic_help: |
| 1167 | fprintf(stdout, "Available commands:\n"); |
| 1168 | |
| 1169 | for (i = 0; commands[i].name; i++) { |
| 1170 | if (commands[i].helpstring != NULL) { |
| 1171 | fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring); |
| 1172 | } |
| 1173 | } |
| 1174 | } else { |
| 1175 | /* print specific help for the selected command */ |
| 1176 | |
| 1177 | /* get the command of the specified name */ |
| 1178 | for (i = 0; commands[i].name; i++) { |
| 1179 | if (strcmp(cmd, commands[i].name) == 0) { |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* execute the command's help if any valid command specified */ |
| 1185 | if (commands[i].name) { |
| 1186 | if (commands[i].help_func != NULL) { |
| 1187 | commands[i].help_func(); |
| 1188 | } else { |
| 1189 | printf("%s\n", commands[i].helpstring); |
| 1190 | } |
| 1191 | } else { |
| 1192 | /* if unknown command specified, print the list of commands */ |
| 1193 | printf("Unknown command \'%s\'\n", cmd); |
| 1194 | goto generic_help; |
| 1195 | } |
| 1196 | } |
| 1197 | |
Radek Krejci | ec797a3 | 2016-02-05 16:52:12 +0100 | [diff] [blame] | 1198 | free(args); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | COMMAND commands[] = { |
| 1203 | {"help", cmd_help, NULL, "Display commands description"}, |
| 1204 | {"add", cmd_add, cmd_add_help, "Add a new model"}, |
| 1205 | {"print", cmd_print, cmd_print_help, "Print model"}, |
Radek Krejci | ba04f3e | 2015-11-10 19:16:01 +0100 | [diff] [blame] | 1206 | {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"}, |
Michal Vasko | a8c77c2 | 2016-02-04 12:08:40 +0100 | [diff] [blame] | 1207 | {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1208 | {"list", cmd_list, cmd_list_help, "List all the loaded models"}, |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 1209 | {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1210 | {"searchpath", cmd_searchpath, cmd_searchpath_help, "Set the search path for models"}, |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 1211 | {"clear", cmd_clear, NULL, "Clear the context - remove all the loaded models"}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 1212 | {"verb", cmd_verb, cmd_verb_help, "Change verbosity"}, |
| 1213 | {"quit", cmd_quit, NULL, "Quit the program"}, |
| 1214 | /* synonyms for previous commands */ |
| 1215 | {"?", cmd_help, NULL, "Display commands description"}, |
| 1216 | {"exit", cmd_quit, NULL, "Quit the program"}, |
| 1217 | {NULL, NULL, NULL, NULL} |
| 1218 | }; |