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