Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file commands.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool commands |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 16 | |
| 17 | #include "commands.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | |
| 19 | #include <ctype.h> |
| 20 | #include <errno.h> |
| 21 | #include <getopt.h> |
| 22 | #include <libgen.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 29 | #include "libyang.h" |
| 30 | |
| 31 | COMMAND commands[]; |
| 32 | extern int done; |
| 33 | extern struct ly_ctx *ctx; |
| 34 | |
| 35 | void |
| 36 | cmd_add_help(void) |
| 37 | { |
| 38 | printf("add [-i] <path-to-model> [<paths-to-other-models> ...]\n"); |
| 39 | printf("\t-i - make all the imported modules implemented\n"); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | cmd_load_help(void) |
| 44 | { |
| 45 | printf("load [-i] <model-name> [<other-model-names> ...]\n"); |
| 46 | printf("\t-i - make all the imported modules implemented\n"); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | cmd_clear_help(void) |
| 51 | { |
| 52 | printf("clear [<yang-library> | -e]\n"); |
| 53 | printf("\t Replace the current context with an empty one, searchpaths are not kept.\n"); |
| 54 | printf("\t If <yang-library> path specified, load the modules according to the yang library data.\n"); |
| 55 | printf("\t Option '-e' causes ietf-yang-library will not be loaded.\n"); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | cmd_print_help(void) |
| 60 | { |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 61 | printf("print [-f (yang | yin | tree [<tree-options>] | info [-P <info-path>] [-(-s)ingle-node])] [-o <output-file>]" |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 62 | " <model-name>[@<revision>]\n"); |
| 63 | printf("\n"); |
| 64 | printf("\ttree-options:\t--tree-print-groupings\t(print top-level groupings in a separate section)\n"); |
| 65 | printf("\t \t--tree-print-uses\t(print uses nodes instead the resolved grouping nodes)\n"); |
| 66 | printf("\t \t--tree-no-leafref-target\t(do not print the target nodes of leafrefs)\n"); |
| 67 | printf("\t \t--tree-path <schema-path>\t(print only the specified subtree)\n"); |
| 68 | printf("\t \t--tree-line-length <line-length>\t(wrap lines if longer than line-length,\n"); |
| 69 | printf("\t \t\tnot a strict limit, longer lines can often appear)\n"); |
| 70 | printf("\n"); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 71 | printf("\tinfo-path:\t<schema-path> | identity/<identity-name> | feature/<feature-name>\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 72 | printf("\n"); |
| 73 | printf("\tschema-path:\t( /<module-name>:<node-identifier> )+\n"); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | cmd_data_help(void) |
| 78 | { |
| 79 | printf("data [-(-s)trict] [-t TYPE] [-d DEFAULTS] [-o <output-file>] [-f (xml | json | lyb)] [-r <running-file-name>]\n"); |
| 80 | printf(" <data-file-name> [<RPC/action-data-file-name> | <yang-data name>]\n\n"); |
| 81 | printf("Accepted TYPEs:\n"); |
| 82 | printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n"); |
| 83 | printf("\t this option is applicable only in case of XML input data.\n"); |
| 84 | printf("\tdata - LYD_OPT_DATA (default value) - complete datastore including status data.\n"); |
| 85 | printf("\tconfig - LYD_OPT_CONFIG - complete configuration datastore.\n"); |
| 86 | printf("\tget - LYD_OPT_GET - <get> operation result.\n"); |
| 87 | printf("\tgetconfig - LYD_OPT_GETCONFIG - <get-config> operation result.\n"); |
| 88 | printf("\tedit - LYD_OPT_EDIT - <edit-config>'s data (content of its <config> element).\n"); |
| 89 | printf("\trpc - LYD_OPT_RPC - NETCONF RPC message.\n"); |
| 90 | printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n"); |
| 91 | printf("\tnotif - LYD_OPT_NOTIF - NETCONF Notification message.\n"); |
| 92 | printf("\tyangdata - LYD_OPT_DATA_TEMPLATE - yang-data extension (last parameter mandatory in this case)\n\n"); |
| 93 | printf("Accepted DEFAULTS:\n"); |
| 94 | printf("\tall - add missing default nodes\n"); |
| 95 | printf("\tall-tagged - add missing default nodes and mark all the default nodes with the attribute.\n"); |
| 96 | printf("\ttrim - remove all nodes with a default value\n"); |
| 97 | printf("\timplicit-tagged - add missing nodes and mark them with the attribute\n\n"); |
| 98 | printf("Option -r:\n"); |
| 99 | printf("\tOptional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the file contains running\n"); |
| 100 | printf("\tconfiguration datastore data referenced from the RPC/Notification. Note that the file is\n"); |
| 101 | printf("\tvalidated as 'data' TYPE. Special value '!' can be used as argument to ignore the\n"); |
| 102 | printf("\texternal references.\n\n"); |
| 103 | printf("\tIf an XPath expression (when/must) needs access to configuration data, you can provide\n"); |
| 104 | printf("\tthem in a file, which will be parsed as 'data' TYPE.\n\n"); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | cmd_xpath_help(void) |
| 109 | { |
| 110 | printf("xpath [-t TYPE] [-x <additional-tree-file-name>] -e <XPath-expression>\n" |
| 111 | " <XML-data-file-name> [<JSON-rpc/action-schema-nodeid>]\n"); |
| 112 | printf("Accepted TYPEs:\n"); |
| 113 | printf("\tauto - resolve data type (one of the following) automatically (as pyang does),\n"); |
| 114 | printf("\t this option is applicable only in case of XML input data.\n"); |
| 115 | printf("\tconfig - LYD_OPT_CONFIG\n"); |
| 116 | printf("\tget - LYD_OPT_GET\n"); |
| 117 | printf("\tgetconfig - LYD_OPT_GETCONFIG\n"); |
| 118 | printf("\tedit - LYD_OPT_EDIT\n"); |
| 119 | printf("\trpc - LYD_OPT_RPC\n"); |
| 120 | printf("\trpcreply - LYD_OPT_RPCREPLY (last parameter mandatory in this case)\n"); |
| 121 | printf("\tnotif - LYD_OPT_NOTIF\n\n"); |
| 122 | printf("Option -x:\n"); |
| 123 | printf("\tIf RPC/action/notification/RPC reply (for TYPEs 'rpc', 'rpcreply', and 'notif') includes\n"); |
| 124 | printf("\tan XPath expression (when/must) that needs access to the configuration data, you can provide\n"); |
| 125 | printf("\tthem in a file, which will be parsed as 'config'.\n"); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | cmd_list_help(void) |
| 130 | { |
| 131 | printf("list [-f (xml | json)]\n\n"); |
| 132 | printf("\tBasic list output (no -f): i - imported module, I - implemented module\n"); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | cmd_feature_help(void) |
| 137 | { |
| 138 | printf("feature [ -(-e)nable | -(-d)isable (* | <feature-name>[,<feature-name> ...]) ] <model-name>[@<revision>]\n"); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | cmd_searchpath_help(void) |
| 143 | { |
| 144 | printf("searchpath [<model-dir-path> | --clear]\n\n"); |
| 145 | printf("\tThey are used to search for imports and includes of a model.\n"); |
| 146 | printf("\tThe \"load\" command uses these directories to find models directly.\n"); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | cmd_verb_help(void) |
| 151 | { |
| 152 | printf("verb (error/0 | warning/1 | verbose/2 | debug/3)\n"); |
| 153 | } |
| 154 | |
| 155 | #ifndef NDEBUG |
| 156 | |
| 157 | void |
| 158 | cmd_debug_help(void) |
| 159 | { |
| 160 | printf("debug (dict | yang | yin | xpath | diff)+\n"); |
| 161 | } |
| 162 | |
| 163 | #endif |
| 164 | |
| 165 | LYS_INFORMAT |
| 166 | get_schema_format(const char *path) |
| 167 | { |
| 168 | char *ptr; |
| 169 | |
| 170 | if ((ptr = strrchr(path, '.')) != NULL) { |
| 171 | ++ptr; |
| 172 | if (!strcmp(ptr, "yang")) { |
| 173 | return LYS_IN_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 174 | } else if (!strcmp(ptr, "yin")) { |
| 175 | return LYS_IN_YIN; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 176 | } else { |
| 177 | fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr); |
| 178 | return LYS_IN_UNKNOWN; |
| 179 | } |
| 180 | } else { |
| 181 | fprintf(stdout, "Input file \"%s\" without extension - unknown format.\n", path); |
| 182 | return LYS_IN_UNKNOWN; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | int |
| 187 | cmd_add(const char *arg) |
| 188 | { |
| 189 | int path_len, ret = 1, index = 0; |
| 190 | char *path, *dir, *s, *arg_ptr; |
| 191 | const char * const *searchpaths; |
| 192 | const struct lys_module *model; |
| 193 | LYS_INFORMAT format = LYS_IN_UNKNOWN; |
| 194 | |
| 195 | if (strlen(arg) < 5) { |
| 196 | cmd_add_help(); |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | arg_ptr = strdup(arg + 3 /* ignore "add" */); |
| 201 | |
| 202 | for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) { |
| 203 | if (s[2] == '\0' || s[2] == ' ') { |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 204 | ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 205 | s[0] = s[1] = ' '; |
| 206 | } |
| 207 | } |
| 208 | s = arg_ptr; |
| 209 | |
| 210 | while (arg_ptr[0] == ' ') { |
| 211 | ++arg_ptr; |
| 212 | } |
| 213 | if (strchr(arg_ptr, ' ')) { |
| 214 | path_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 215 | } else { |
| 216 | path_len = strlen(arg_ptr); |
| 217 | } |
| 218 | path = strndup(arg_ptr, path_len); |
| 219 | |
| 220 | searchpaths = ly_ctx_get_searchdirs(ctx); |
| 221 | if (searchpaths) { |
| 222 | for (index = 0; searchpaths[index]; index++); |
| 223 | } |
| 224 | |
| 225 | while (path) { |
| 226 | format = get_schema_format(path); |
| 227 | if (format == LYS_IN_UNKNOWN) { |
| 228 | free(path); |
| 229 | goto cleanup; |
| 230 | } |
| 231 | |
| 232 | dir = strdup(path); |
| 233 | ly_ctx_set_searchdir(ctx, dirname(dir)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 234 | lys_parse_path(ctx, path, format, &model); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 235 | ly_ctx_unset_searchdir(ctx, index); |
| 236 | free(path); |
| 237 | free(dir); |
| 238 | |
| 239 | if (!model) { |
| 240 | /* libyang printed the error messages */ |
| 241 | goto cleanup; |
| 242 | } |
| 243 | |
| 244 | /* next model */ |
| 245 | arg_ptr += path_len; |
| 246 | while (arg_ptr[0] == ' ') { |
| 247 | ++arg_ptr; |
| 248 | } |
| 249 | if (strchr(arg_ptr, ' ')) { |
| 250 | path_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 251 | } else { |
| 252 | path_len = strlen(arg_ptr); |
| 253 | } |
| 254 | |
| 255 | if (path_len) { |
| 256 | path = strndup(arg_ptr, path_len); |
| 257 | } else { |
| 258 | path = NULL; |
| 259 | } |
| 260 | } |
| 261 | if (format == LYS_IN_UNKNOWN) { |
| 262 | /* no schema on input */ |
| 263 | cmd_add_help(); |
| 264 | goto cleanup; |
| 265 | } |
| 266 | ret = 0; |
| 267 | |
| 268 | cleanup: |
| 269 | free(s); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 270 | ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | int |
| 276 | cmd_load(const char *arg) |
| 277 | { |
| 278 | int name_len, ret = 1; |
| 279 | char *name, *s, *arg_ptr; |
| 280 | const struct lys_module *model; |
| 281 | |
| 282 | if (strlen(arg) < 6) { |
| 283 | cmd_load_help(); |
| 284 | return 1; |
| 285 | } |
| 286 | |
| 287 | arg_ptr = strdup(arg + 4 /* ignore "load" */); |
| 288 | |
| 289 | for (s = strstr(arg_ptr, "-i"); s ; s = strstr(s + 2, "-i")) { |
| 290 | if (s[2] == '\0' || s[2] == ' ') { |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 291 | ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 292 | s[0] = s[1] = ' '; |
| 293 | } |
| 294 | } |
| 295 | s = arg_ptr; |
| 296 | |
| 297 | while (arg_ptr[0] == ' ') { |
| 298 | ++arg_ptr; |
| 299 | } |
| 300 | if (strchr(arg_ptr, ' ')) { |
| 301 | name_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 302 | } else { |
| 303 | name_len = strlen(arg_ptr); |
| 304 | } |
| 305 | name = strndup(arg_ptr, name_len); |
| 306 | |
| 307 | while (name) { |
| 308 | model = ly_ctx_load_module(ctx, name, NULL); |
| 309 | free(name); |
| 310 | if (!model) { |
| 311 | /* libyang printed the error messages */ |
| 312 | goto cleanup; |
| 313 | } |
| 314 | |
| 315 | /* next model */ |
| 316 | arg_ptr += name_len; |
| 317 | while (arg_ptr[0] == ' ') { |
| 318 | ++arg_ptr; |
| 319 | } |
| 320 | if (strchr(arg_ptr, ' ')) { |
| 321 | name_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 322 | } else { |
| 323 | name_len = strlen(arg_ptr); |
| 324 | } |
| 325 | |
| 326 | if (name_len) { |
| 327 | name = strndup(arg_ptr, name_len); |
| 328 | } else { |
| 329 | name = NULL; |
| 330 | } |
| 331 | } |
| 332 | ret = 0; |
| 333 | |
| 334 | cleanup: |
| 335 | free(s); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 336 | ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | int |
| 342 | cmd_print(const char *arg) |
| 343 | { |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 344 | int c, argc, option_index, ret = 1, tree_ll = 0, output_opts = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 345 | char **argv = NULL, *ptr, *model_name, *revision; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 346 | const char *out_path = NULL, *target_path = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 347 | const struct lys_module *module; |
| 348 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 349 | struct ly_out *out = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 350 | static struct option long_options[] = { |
| 351 | {"help", no_argument, 0, 'h'}, |
| 352 | {"format", required_argument, 0, 'f'}, |
| 353 | {"output", required_argument, 0, 'o'}, |
| 354 | #if 0 |
| 355 | {"tree-print-groupings", no_argument, 0, 'g'}, |
| 356 | {"tree-print-uses", no_argument, 0, 'u'}, |
| 357 | {"tree-no-leafref-target", no_argument, 0, 'n'}, |
| 358 | {"tree-path", required_argument, 0, 'P'}, |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 359 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 360 | {"info-path", required_argument, 0, 'P'}, |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 361 | {"single-node", no_argument, 0, 's'}, |
| 362 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 363 | {"tree-line-length", required_argument, 0, 'L'}, |
| 364 | #endif |
| 365 | {NULL, 0, 0, 0} |
| 366 | }; |
| 367 | void *rlcd; |
| 368 | |
| 369 | argc = 1; |
| 370 | argv = malloc(2*sizeof *argv); |
| 371 | *argv = strdup(arg); |
| 372 | ptr = strtok(*argv, " "); |
| 373 | while ((ptr = strtok(NULL, " "))) { |
| 374 | rlcd = realloc(argv, (argc+2)*sizeof *argv); |
| 375 | if (!rlcd) { |
| 376 | fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 377 | goto cleanup; |
| 378 | } |
| 379 | argv = rlcd; |
| 380 | argv[argc++] = ptr; |
| 381 | } |
| 382 | argv[argc] = NULL; |
| 383 | |
| 384 | optind = 0; |
| 385 | while (1) { |
| 386 | option_index = 0; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 387 | c = getopt_long(argc, argv, "chf:go:guP:sL:", long_options, &option_index); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 388 | if (c == -1) { |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | switch (c) { |
| 393 | case 'h': |
| 394 | cmd_print_help(); |
| 395 | ret = 0; |
| 396 | goto cleanup; |
| 397 | case 'f': |
| 398 | if (!strcmp(optarg, "yang")) { |
| 399 | format = LYS_OUT_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 400 | } else if (!strcmp(optarg, "yin")) { |
| 401 | format = LYS_OUT_YIN; |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 402 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 403 | } else if (!strcmp(optarg, "tree")) { |
| 404 | format = LYS_OUT_TREE; |
| 405 | } else if (!strcmp(optarg, "tree-rfc")) { |
| 406 | format = LYS_OUT_TREE; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 407 | output_opts |= LYS_OUTOPT_TREE_RFC; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 408 | #endif |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 409 | } else if (!strcmp(optarg, "info")) { |
| 410 | format = LYS_OUT_YANG_COMPILED; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 411 | } else { |
| 412 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 413 | goto cleanup; |
| 414 | } |
| 415 | break; |
| 416 | case 'o': |
| 417 | if (out_path) { |
| 418 | fprintf(stderr, "Output specified twice.\n"); |
| 419 | goto cleanup; |
| 420 | } |
| 421 | out_path = optarg; |
| 422 | break; |
| 423 | #if 0 |
| 424 | case 'g': |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 425 | output_opts |= LYS_OUTOPT_TREE_GROUPING; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 426 | break; |
| 427 | case 'u': |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 428 | output_opts |= LYS_OUTOPT_TREE_USES; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 429 | break; |
| 430 | case 'n': |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 431 | output_opts |= LYS_OUTOPT_TREE_NO_LEAFREF; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 432 | break; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 433 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 434 | case 'P': |
| 435 | target_path = optarg; |
| 436 | break; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 437 | case 's': |
Radek Krejci | 4fa6ebf | 2019-11-21 13:34:35 +0800 | [diff] [blame] | 438 | output_opts |= LYS_OUTPUT_NO_SUBSTMT; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 439 | break; |
| 440 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 441 | case 'L': |
| 442 | tree_ll = atoi(optarg); |
| 443 | break; |
| 444 | #endif |
| 445 | case '?': |
| 446 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 447 | goto cleanup; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /* file name */ |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 452 | if (optind == argc && !target_path) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 453 | fprintf(stderr, "Missing the module name.\n"); |
| 454 | goto cleanup; |
| 455 | } |
| 456 | |
Radek Krejci | 77954e8 | 2019-04-30 13:51:29 +0200 | [diff] [blame] | 457 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 458 | /* tree fromat with or without gropings */ |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 459 | if ((output_opts || tree_ll) && format != LYS_OUT_TREE) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 460 | fprintf(stderr, "--tree options take effect only in case of the tree output format.\n"); |
| 461 | } |
Radek Krejci | 77954e8 | 2019-04-30 13:51:29 +0200 | [diff] [blame] | 462 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 463 | |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 464 | if (!target_path) { |
| 465 | /* module, revision */ |
| 466 | model_name = argv[optind]; |
| 467 | revision = NULL; |
| 468 | if (strchr(model_name, '@')) { |
| 469 | revision = strchr(model_name, '@'); |
| 470 | revision[0] = '\0'; |
| 471 | ++revision; |
| 472 | } |
| 473 | |
| 474 | if (revision) { |
| 475 | module = ly_ctx_get_module(ctx, model_name, revision); |
| 476 | } else { |
| 477 | module = ly_ctx_get_module_latest(ctx, model_name); |
| 478 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 479 | #if 0 |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 480 | if (!module) { |
| 481 | /* not a module, try to find it as a submodule */ |
| 482 | module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision); |
| 483 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 484 | #endif |
| 485 | |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 486 | if (!module) { |
| 487 | if (revision) { |
| 488 | fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision); |
| 489 | } else { |
| 490 | fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name); |
| 491 | } |
| 492 | goto cleanup; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 493 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (out_path) { |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 497 | ret = ly_out_new_filepath(out_path, &out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 498 | } else { |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 499 | ret = ly_out_new_file(stdout, &out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 500 | } |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 501 | if (ret) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 502 | fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno)); |
| 503 | goto cleanup; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 506 | if (target_path) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 507 | const struct lysc_node *node = lys_find_node(ctx, NULL, target_path); |
| 508 | if (node) { |
| 509 | ret = lys_print_node(out, node, format, tree_ll, output_opts); |
| 510 | } else { |
| 511 | fprintf(stderr, "The requested schema node \"%s\" does not exists.\n", target_path); |
| 512 | } |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 513 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 514 | ret = lys_print_module(out, module, format, tree_ll, output_opts); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | cleanup: |
| 518 | free(*argv); |
| 519 | free(argv); |
| 520 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 521 | ly_out_free(out, NULL, out_path ? 1 : 0); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 522 | |
| 523 | return ret; |
| 524 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 525 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 526 | static int |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 527 | parse_data(char *filepath, int *options, const struct lyd_node *tree, const char *rpc_act_file, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 528 | struct lyd_node **result) |
| 529 | { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 530 | struct lyd_node *data = NULL, *rpc_act = NULL; |
| 531 | int opts = *options; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 532 | struct ly_in *in; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 533 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 534 | if (ly_in_new_filepath(filepath, 0, &in)) { |
| 535 | fprintf(stderr, "Unable to open input YANG data file \"%s\".", filepath); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 536 | return EXIT_FAILURE; |
| 537 | } |
| 538 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 539 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 540 | if ((opts & LYD_OPT_TYPEMASK) == LYD_OPT_TYPEMASK) { |
| 541 | /* automatically detect data type from the data top level */ |
| 542 | if (informat != LYD_XML) { |
| 543 | fprintf(stderr, "Only XML data can be automatically explored.\n"); |
| 544 | return EXIT_FAILURE; |
| 545 | } |
| 546 | |
| 547 | xml = lyxml_parse_path(ctx, filepath, 0); |
| 548 | if (!xml) { |
| 549 | fprintf(stderr, "Failed to parse XML data for automatic type detection.\n"); |
| 550 | return EXIT_FAILURE; |
| 551 | } |
| 552 | |
| 553 | /* NOTE: namespace is ignored to simplify usage of this feature */ |
| 554 | |
| 555 | if (!strcmp(xml->name, "data")) { |
| 556 | fprintf(stdout, "Parsing %s as complete datastore.\n", filepath); |
| 557 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_ADD_YANGLIB; |
| 558 | } else if (!strcmp(xml->name, "config")) { |
| 559 | fprintf(stdout, "Parsing %s as config data.\n", filepath); |
| 560 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 561 | } else if (!strcmp(xml->name, "get-reply")) { |
| 562 | fprintf(stdout, "Parsing %s as <get> reply data.\n", filepath); |
| 563 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 564 | } else if (!strcmp(xml->name, "get-config-reply")) { |
| 565 | fprintf(stdout, "Parsing %s as <get-config> reply data.\n", filepath); |
| 566 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 567 | } else if (!strcmp(xml->name, "edit-config")) { |
| 568 | fprintf(stdout, "Parsing %s as <edit-config> data.\n", filepath); |
| 569 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 570 | } else if (!strcmp(xml->name, "rpc")) { |
| 571 | fprintf(stdout, "Parsing %s as <rpc> data.\n", filepath); |
| 572 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 573 | } else if (!strcmp(xml->name, "rpc-reply")) { |
| 574 | if (!rpc_act_file) { |
| 575 | fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n"); |
| 576 | lyxml_free(ctx, xml); |
| 577 | return EXIT_FAILURE; |
| 578 | } |
| 579 | fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", filepath); |
| 580 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY; |
| 581 | rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, val_tree); |
| 582 | if (!rpc_act) { |
| 583 | fprintf(stderr, "Failed to parse RPC/action.\n"); |
| 584 | lyxml_free(ctx, xml); |
| 585 | return EXIT_FAILURE; |
| 586 | } |
| 587 | } else if (!strcmp(xml->name, "notification")) { |
| 588 | fprintf(stdout, "Parsing %s as <notification> data.\n", filepath); |
| 589 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 590 | } else if (!strcmp(xml->name, "yang-data")) { |
| 591 | fprintf(stdout, "Parsing %s as <yang-data> data.\n", filepath); |
| 592 | opts = (opts & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE; |
| 593 | if (!rpc_act_file) { |
| 594 | fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n"); |
| 595 | lyxml_free(ctx, xml); |
| 596 | return EXIT_FAILURE; |
| 597 | } |
| 598 | } else { |
| 599 | fprintf(stderr, "Invalid top-level element for automatic data type recognition.\n"); |
| 600 | lyxml_free(ctx, xml); |
| 601 | return EXIT_FAILURE; |
| 602 | } |
| 603 | |
| 604 | if (opts & LYD_OPT_RPCREPLY) { |
| 605 | data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act, val_tree); |
| 606 | } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) { |
| 607 | data = lyd_parse_xml(ctx, &xml->child, opts, val_tree); |
| 608 | } else if (opts & LYD_OPT_DATA_TEMPLATE) { |
| 609 | data = lyd_parse_xml(ctx, &xml->child, opts, rpc_act_file); |
| 610 | } else { |
| 611 | data = lyd_parse_xml(ctx, &xml->child, opts); |
| 612 | } |
| 613 | lyxml_free(ctx, xml); |
| 614 | } else { |
| 615 | if (opts & LYD_OPT_RPCREPLY) { |
| 616 | if (!rpc_act_file) { |
| 617 | fprintf(stderr, "RPC/action reply data require additional argument (file with the RPC/action).\n"); |
| 618 | return EXIT_FAILURE; |
| 619 | } |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 620 | rpc_act = lyd_parse_path(ctx, rpc_act_file, informat, LYD_OPT_RPC, trees); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 621 | if (!rpc_act) { |
| 622 | fprintf(stderr, "Failed to parse RPC/action.\n"); |
| 623 | return EXIT_FAILURE; |
| 624 | } |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 625 | if (trees) { |
| 626 | const struct lyd_node **trees_new; |
| 627 | unsigned int u; |
| 628 | trees_new = lyd_trees_new(1, rpc_act); |
| 629 | |
| 630 | LY_ARRAY_FOR(trees, u) { |
| 631 | trees_new = lyd_trees_add(trees_new, trees[u]); |
| 632 | } |
| 633 | lyd_trees_free(trees, 0); |
| 634 | trees = trees_new; |
| 635 | } else { |
| 636 | trees = lyd_trees_new(1, rpc_act); |
| 637 | } |
| 638 | data = lyd_parse_path(ctx, filepath, informat, opts, trees); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 639 | } else if (opts & (LYD_OPT_RPC | LYD_OPT_NOTIF)) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 640 | data = lyd_parse_path(ctx, filepath, informat, opts, trees); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 641 | } else if (opts & LYD_OPT_DATA_TEMPLATE) { |
| 642 | if (!rpc_act_file) { |
| 643 | fprintf(stderr, "YANG-DATA require additional argument (name instance of yang-data extension).\n"); |
| 644 | return EXIT_FAILURE; |
| 645 | } |
| 646 | data = lyd_parse_path(ctx, filepath, informat, opts, rpc_act_file); |
| 647 | } else { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 648 | #endif |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 649 | |
| 650 | lyd_parse_data(ctx, in, 0, opts, 0, &data); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 651 | #if 0 |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 652 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 653 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 654 | #endif |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 655 | ly_in_free(in, 0); |
| 656 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 657 | lyd_free_all(rpc_act); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 658 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 659 | if (ly_err_first(ctx)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 660 | fprintf(stderr, "Failed to parse data.\n"); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 661 | lyd_free_all(data); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 662 | return EXIT_FAILURE; |
| 663 | } |
| 664 | |
| 665 | *result = data; |
| 666 | *options = opts; |
| 667 | return EXIT_SUCCESS; |
| 668 | } |
| 669 | |
| 670 | int |
| 671 | cmd_data(const char *arg) |
| 672 | { |
| 673 | int c, argc, option_index, ret = 1; |
| 674 | int options = 0, printopt = 0; |
| 675 | char **argv = NULL, *ptr; |
| 676 | const char *out_path = NULL; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 677 | struct lyd_node *data = NULL; |
| 678 | struct lyd_node *tree = NULL; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 679 | LYD_FORMAT outformat = 0; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 680 | struct ly_out *out = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 681 | static struct option long_options[] = { |
| 682 | {"defaults", required_argument, 0, 'd'}, |
| 683 | {"help", no_argument, 0, 'h'}, |
| 684 | {"format", required_argument, 0, 'f'}, |
| 685 | {"option", required_argument, 0, 't'}, |
| 686 | {"output", required_argument, 0, 'o'}, |
| 687 | {"running", required_argument, 0, 'r'}, |
| 688 | {"strict", no_argument, 0, 's'}, |
| 689 | {NULL, 0, 0, 0} |
| 690 | }; |
| 691 | void *rlcd; |
| 692 | |
| 693 | argc = 1; |
| 694 | argv = malloc(2*sizeof *argv); |
| 695 | *argv = strdup(arg); |
| 696 | ptr = strtok(*argv, " "); |
| 697 | while ((ptr = strtok(NULL, " "))) { |
| 698 | rlcd = realloc(argv, (argc + 2) * sizeof *argv); |
| 699 | if (!rlcd) { |
| 700 | fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 701 | goto cleanup; |
| 702 | } |
| 703 | argv = rlcd; |
| 704 | argv[argc++] = ptr; |
| 705 | } |
| 706 | argv[argc] = NULL; |
| 707 | |
| 708 | optind = 0; |
| 709 | while (1) { |
| 710 | option_index = 0; |
| 711 | c = getopt_long(argc, argv, "d:hf:o:st:r:", long_options, &option_index); |
| 712 | if (c == -1) { |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | switch (c) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 717 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 718 | case 'd': |
| 719 | if (!strcmp(optarg, "all")) { |
| 720 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL; |
| 721 | } else if (!strcmp(optarg, "all-tagged")) { |
| 722 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG; |
| 723 | } else if (!strcmp(optarg, "trim")) { |
| 724 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_TRIM; |
| 725 | } else if (!strcmp(optarg, "implicit-tagged")) { |
| 726 | printopt = (printopt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG; |
| 727 | } |
| 728 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 729 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 730 | case 'h': |
| 731 | cmd_data_help(); |
| 732 | ret = 0; |
| 733 | goto cleanup; |
| 734 | case 'f': |
| 735 | if (!strcmp(optarg, "xml")) { |
| 736 | outformat = LYD_XML; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 737 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 738 | } else if (!strcmp(optarg, "json")) { |
| 739 | outformat = LYD_JSON; |
| 740 | } else if (!strcmp(optarg, "lyb")) { |
| 741 | outformat = LYD_LYB; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 742 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 743 | } else { |
| 744 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 745 | goto cleanup; |
| 746 | } |
| 747 | break; |
| 748 | case 'o': |
| 749 | if (out_path) { |
| 750 | fprintf(stderr, "Output specified twice.\n"); |
| 751 | goto cleanup; |
| 752 | } |
| 753 | out_path = optarg; |
| 754 | break; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 755 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 756 | case 'r': |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 757 | if (optarg[0] == '!') { |
| 758 | /* ignore extenral dependencies to the running datastore */ |
| 759 | options |= LYD_OPT_NOEXTDEPS; |
| 760 | } else { |
| 761 | /* external file with the running datastore */ |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 762 | val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_DATA_NO_YANGLIB, trees); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 763 | if (!val_tree) { |
| 764 | fprintf(stderr, "Failed to parse the additional data tree for validation.\n"); |
| 765 | goto cleanup; |
| 766 | } |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 767 | if (!trees) { |
| 768 | trees = lyd_trees_new(1, val_tree); |
| 769 | } else { |
| 770 | trees = lyd_trees_add(trees, val_tree); |
| 771 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 772 | } |
| 773 | break; |
| 774 | case 's': |
| 775 | options |= LYD_OPT_STRICT; |
| 776 | options |= LYD_OPT_OBSOLETE; |
| 777 | break; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 778 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 779 | case 't': |
| 780 | if (!strcmp(optarg, "auto")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 781 | /* no flags */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 782 | } else if (!strcmp(optarg, "data")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 783 | /* no flags */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 784 | /*} else if (!strcmp(optarg, "config")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 785 | options |= LYD_OPT_CONFIG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 786 | } else if (!strcmp(optarg, "get")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 787 | options |= LYD_OPT_GET; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 788 | } else if (!strcmp(optarg, "getconfig")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 789 | options |= LYD_OPT_GETCONFIG; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 790 | } else if (!strcmp(optarg, "edit")) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 791 | options |= LYD_OPT_EDIT;*/ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 792 | } else { |
| 793 | fprintf(stderr, "Invalid parser option \"%s\".\n", optarg); |
| 794 | cmd_data_help(); |
| 795 | goto cleanup; |
| 796 | } |
| 797 | break; |
| 798 | case '?': |
| 799 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 800 | goto cleanup; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* file name */ |
| 805 | if (optind == argc) { |
| 806 | fprintf(stderr, "Missing the data file name.\n"); |
| 807 | goto cleanup; |
| 808 | } |
| 809 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 810 | if (parse_data(argv[optind], &options, tree, argv[optind + 1], &data)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 811 | goto cleanup; |
| 812 | } |
| 813 | |
| 814 | if (out_path) { |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 815 | ret = ly_out_new_filepath(out_path, &out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 816 | } else { |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 817 | ret = ly_out_new_file(stdout, &out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 818 | } |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 819 | if (ret) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 820 | fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno)); |
| 821 | goto cleanup; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 824 | if (outformat) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 825 | ret = lyd_print_all(out, data, outformat, LYD_PRINT_FORMAT | printopt); |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 826 | ret = ret < 0 ? ret * (-1) : 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 827 | } |
| 828 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 829 | cleanup: |
| 830 | free(*argv); |
| 831 | free(argv); |
| 832 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 833 | ly_out_free(out, NULL, out_path ? 1 : 0); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 834 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 835 | lyd_free_all(data); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 836 | |
| 837 | return ret; |
| 838 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 839 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 840 | int |
| 841 | cmd_xpath(const char *arg) |
| 842 | { |
| 843 | int c, argc, option_index, ret = 1, long_str; |
| 844 | char **argv = NULL, *ptr, *expr = NULL; |
| 845 | unsigned int i, j; |
| 846 | int options = 0; |
| 847 | struct lyd_node *data = NULL, *node, *val_tree = NULL; |
| 848 | struct lyd_node_leaf_list *key; |
| 849 | struct ly_set *set; |
| 850 | static struct option long_options[] = { |
| 851 | {"help", no_argument, 0, 'h'}, |
| 852 | {"expr", required_argument, 0, 'e'}, |
| 853 | {NULL, 0, 0, 0} |
| 854 | }; |
| 855 | void *rlcd; |
| 856 | |
| 857 | long_str = 0; |
| 858 | argc = 1; |
| 859 | argv = malloc(2 * sizeof *argv); |
| 860 | *argv = strdup(arg); |
| 861 | ptr = strtok(*argv, " "); |
| 862 | while ((ptr = strtok(NULL, " "))) { |
| 863 | if (long_str) { |
| 864 | ptr[-1] = ' '; |
| 865 | if (ptr[strlen(ptr) - 1] == long_str) { |
| 866 | long_str = 0; |
| 867 | ptr[strlen(ptr) - 1] = '\0'; |
| 868 | } |
| 869 | } else { |
| 870 | rlcd = realloc(argv, (argc + 2) * sizeof *argv); |
| 871 | if (!rlcd) { |
| 872 | fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 873 | goto cleanup; |
| 874 | } |
| 875 | argv = rlcd; |
| 876 | argv[argc] = ptr; |
| 877 | if (ptr[0] == '"') { |
| 878 | long_str = '"'; |
| 879 | ++argv[argc]; |
| 880 | } |
| 881 | if (ptr[0] == '\'') { |
| 882 | long_str = '\''; |
| 883 | ++argv[argc]; |
| 884 | } |
| 885 | if (ptr[strlen(ptr) - 1] == long_str) { |
| 886 | long_str = 0; |
| 887 | ptr[strlen(ptr) - 1] = '\0'; |
| 888 | } |
| 889 | ++argc; |
| 890 | } |
| 891 | } |
| 892 | argv[argc] = NULL; |
| 893 | |
| 894 | optind = 0; |
| 895 | while (1) { |
| 896 | option_index = 0; |
| 897 | c = getopt_long(argc, argv, "he:t:x:", long_options, &option_index); |
| 898 | if (c == -1) { |
| 899 | break; |
| 900 | } |
| 901 | |
| 902 | switch (c) { |
| 903 | case 'h': |
| 904 | cmd_xpath_help(); |
| 905 | ret = 0; |
| 906 | goto cleanup; |
| 907 | case 'e': |
| 908 | expr = optarg; |
| 909 | break; |
| 910 | case 't': |
| 911 | if (!strcmp(optarg, "auto")) { |
| 912 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_TYPEMASK; |
| 913 | } else if (!strcmp(optarg, "config")) { |
| 914 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 915 | } else if (!strcmp(optarg, "get")) { |
| 916 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 917 | } else if (!strcmp(optarg, "getconfig")) { |
| 918 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 919 | } else if (!strcmp(optarg, "edit")) { |
| 920 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 921 | } else if (!strcmp(optarg, "rpc")) { |
| 922 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 923 | } else if (!strcmp(optarg, "rpcreply")) { |
| 924 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY; |
| 925 | } else if (!strcmp(optarg, "notif")) { |
| 926 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 927 | } else if (!strcmp(optarg, "yangdata")) { |
| 928 | options = (options & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_TEMPLATE; |
| 929 | } else { |
| 930 | fprintf(stderr, "Invalid parser option \"%s\".\n", optarg); |
| 931 | cmd_data_help(); |
| 932 | goto cleanup; |
| 933 | } |
| 934 | break; |
| 935 | case 'x': |
| 936 | val_tree = lyd_parse_path(ctx, optarg, LYD_XML, LYD_OPT_CONFIG); |
| 937 | if (!val_tree) { |
| 938 | fprintf(stderr, "Failed to parse the additional data tree for validation.\n"); |
| 939 | goto cleanup; |
| 940 | } |
| 941 | break; |
| 942 | case '?': |
| 943 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 944 | goto cleanup; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | if (optind == argc) { |
| 949 | fprintf(stderr, "Missing the file with data.\n"); |
| 950 | goto cleanup; |
| 951 | } |
| 952 | |
| 953 | if (!expr) { |
| 954 | fprintf(stderr, "Missing the XPath expression.\n"); |
| 955 | goto cleanup; |
| 956 | } |
| 957 | |
| 958 | if (parse_data(argv[optind], &options, val_tree, argv[optind + 1], &data)) { |
| 959 | goto cleanup; |
| 960 | } |
| 961 | |
| 962 | if (!(set = lyd_find_path(data, expr))) { |
| 963 | goto cleanup; |
| 964 | } |
| 965 | |
| 966 | /* print result */ |
| 967 | printf("Result:\n"); |
| 968 | if (!set->number) { |
| 969 | printf("\tEmpty\n"); |
| 970 | } else { |
| 971 | for (i = 0; i < set->number; ++i) { |
| 972 | node = set->set.d[i]; |
| 973 | switch (node->schema->nodetype) { |
| 974 | case LYS_CONTAINER: |
| 975 | printf("\tContainer "); |
| 976 | break; |
| 977 | case LYS_LEAF: |
| 978 | printf("\tLeaf "); |
| 979 | break; |
| 980 | case LYS_LEAFLIST: |
| 981 | printf("\tLeaflist "); |
| 982 | break; |
| 983 | case LYS_LIST: |
| 984 | printf("\tList "); |
| 985 | break; |
| 986 | case LYS_ANYXML: |
| 987 | printf("\tAnyxml "); |
| 988 | break; |
| 989 | case LYS_ANYDATA: |
| 990 | printf("\tAnydata "); |
| 991 | break; |
| 992 | default: |
| 993 | printf("\tUnknown "); |
| 994 | break; |
| 995 | } |
| 996 | printf("\"%s\"", node->schema->name); |
| 997 | if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 998 | printf(" (val: %s)", ((struct lyd_node_leaf_list *)node)->value_str); |
| 999 | } else if (node->schema->nodetype == LYS_LIST) { |
| 1000 | key = (struct lyd_node_leaf_list *)node->child; |
| 1001 | printf(" ("); |
| 1002 | for (j = 0; j < ((struct lys_node_list *)node->schema)->keys_size; ++j) { |
| 1003 | if (j) { |
| 1004 | printf(" "); |
| 1005 | } |
| 1006 | printf("\"%s\": %s", key->schema->name, key->value_str); |
| 1007 | key = (struct lyd_node_leaf_list *)key->next; |
| 1008 | } |
| 1009 | printf(")"); |
| 1010 | } |
| 1011 | printf("\n"); |
| 1012 | } |
| 1013 | } |
| 1014 | printf("\n"); |
| 1015 | |
| 1016 | ly_set_free(set); |
| 1017 | ret = 0; |
| 1018 | |
| 1019 | cleanup: |
| 1020 | free(*argv); |
| 1021 | free(argv); |
| 1022 | |
| 1023 | lyd_free_withsiblings(data); |
| 1024 | |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | int |
| 1029 | print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat) |
| 1030 | { |
| 1031 | struct lyd_node *ylib; |
| 1032 | uint32_t idx = 0, has_modules = 0; |
| 1033 | uint8_t u; |
| 1034 | const struct lys_module *mod; |
| 1035 | |
| 1036 | if (outformat != LYD_UNKNOWN) { |
| 1037 | ylib = ly_ctx_info(ctx); |
| 1038 | if (!ylib) { |
| 1039 | fprintf(stderr, "Getting context info (ietf-yang-library data) failed.\n"); |
| 1040 | return 1; |
| 1041 | } |
| 1042 | |
| 1043 | lyd_print_file(out, ylib, outformat, LYP_WITHSIBLINGS | LYP_FORMAT); |
| 1044 | lyd_free_withsiblings(ylib); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | /* iterate schemas in context and provide just the basic info */ |
| 1049 | fprintf(out, "List of the loaded models:\n"); |
| 1050 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
| 1051 | has_modules++; |
| 1052 | |
| 1053 | /* conformance print */ |
| 1054 | if (mod->implemented) { |
| 1055 | fprintf(out, "\tI"); |
| 1056 | } else { |
| 1057 | fprintf(out, "\ti"); |
| 1058 | } |
| 1059 | |
| 1060 | /* module print */ |
| 1061 | fprintf(out, " %s", mod->name); |
| 1062 | if (mod->rev_size) { |
| 1063 | fprintf(out, "@%s", mod->rev[0].date); |
| 1064 | } |
| 1065 | |
| 1066 | /* submodules print */ |
| 1067 | if (mod->inc_size) { |
| 1068 | fprintf(out, " ("); |
| 1069 | for (u = 0; u < mod->inc_size; u++) { |
| 1070 | fprintf(out, "%s%s", !u ? "" : ",", mod->inc[u].submodule->name); |
| 1071 | if (mod->inc[u].submodule->rev_size) { |
| 1072 | fprintf(out, "@%s", mod->inc[u].submodule->rev[0].date); |
| 1073 | } |
| 1074 | } |
| 1075 | fprintf(out, ")"); |
| 1076 | } |
| 1077 | |
| 1078 | /* finish the line */ |
| 1079 | fprintf(out, "\n"); |
| 1080 | } |
| 1081 | |
| 1082 | if (!has_modules) { |
| 1083 | fprintf(out, "\t(none)\n"); |
| 1084 | } |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | int |
| 1090 | cmd_list(const char *arg) |
| 1091 | { |
| 1092 | char **argv = NULL, *ptr; |
| 1093 | int c, argc, option_index; |
| 1094 | LYD_FORMAT outformat = LYD_UNKNOWN; |
| 1095 | static struct option long_options[] = { |
| 1096 | {"help", no_argument, 0, 'h'}, |
| 1097 | {"format", required_argument, 0, 'f'}, |
| 1098 | {NULL, 0, 0, 0} |
| 1099 | }; |
| 1100 | void *rlcd; |
| 1101 | |
| 1102 | argc = 1; |
| 1103 | argv = malloc(2*sizeof *argv); |
| 1104 | *argv = strdup(arg); |
| 1105 | ptr = strtok(*argv, " "); |
| 1106 | while ((ptr = strtok(NULL, " "))) { |
| 1107 | rlcd = realloc(argv, (argc+2)*sizeof *argv); |
| 1108 | if (!rlcd) { |
| 1109 | fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 1110 | goto error; |
| 1111 | } |
| 1112 | argv = rlcd; |
| 1113 | argv[argc++] = ptr; |
| 1114 | } |
| 1115 | argv[argc] = NULL; |
| 1116 | |
| 1117 | optind = 0; |
| 1118 | while (1) { |
| 1119 | option_index = 0; |
| 1120 | c = getopt_long(argc, argv, "hf:", long_options, &option_index); |
| 1121 | if (c == -1) { |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | switch (c) { |
| 1126 | case 'h': |
| 1127 | cmd_data_help(); |
| 1128 | free(*argv); |
| 1129 | free(argv); |
| 1130 | return 0; |
| 1131 | case 'f': |
| 1132 | if (!strcmp(optarg, "xml")) { |
| 1133 | outformat = LYD_XML; |
| 1134 | } else if (!strcmp(optarg, "json")) { |
| 1135 | outformat = LYD_JSON; |
| 1136 | } else { |
| 1137 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 1138 | goto error; |
| 1139 | } |
| 1140 | break; |
| 1141 | case '?': |
| 1142 | /* getopt_long() prints message */ |
| 1143 | goto error; |
| 1144 | } |
| 1145 | } |
| 1146 | if (optind != argc) { |
| 1147 | fprintf(stderr, "Unknown parameter \"%s\"\n", argv[optind]); |
| 1148 | error: |
| 1149 | free(*argv); |
| 1150 | free(argv); |
| 1151 | return 1; |
| 1152 | } |
| 1153 | free(*argv); |
| 1154 | free(argv); |
| 1155 | |
| 1156 | return print_list(stdout, ctx, outformat); |
| 1157 | } |
| 1158 | #endif |
| 1159 | int |
| 1160 | cmd_feature(const char *arg) |
| 1161 | { |
| 1162 | int c, argc, option_index, ret = 1, task = 0; |
| 1163 | char **argv = NULL, *ptr, *model_name, *revision, *feat_names = NULL; |
| 1164 | const struct lys_module *module; |
| 1165 | static struct option long_options[] = { |
| 1166 | {"help", no_argument, 0, 'h'}, |
| 1167 | {"enable", required_argument, 0, 'e'}, |
| 1168 | {"disable", required_argument, 0, 'd'}, |
| 1169 | {NULL, 0, 0, 0} |
| 1170 | }; |
| 1171 | void *rlcd; |
| 1172 | |
| 1173 | argc = 1; |
| 1174 | argv = malloc(2*sizeof *argv); |
| 1175 | *argv = strdup(arg); |
| 1176 | ptr = strtok(*argv, " "); |
| 1177 | while ((ptr = strtok(NULL, " "))) { |
| 1178 | rlcd = realloc(argv, (argc + 2) * sizeof *argv); |
| 1179 | if (!rlcd) { |
| 1180 | fprintf(stderr, "Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 1181 | goto cleanup; |
| 1182 | } |
| 1183 | argv = rlcd; |
| 1184 | argv[argc++] = ptr; |
| 1185 | } |
| 1186 | argv[argc] = NULL; |
| 1187 | |
| 1188 | optind = 0; |
| 1189 | while (1) { |
| 1190 | option_index = 0; |
| 1191 | c = getopt_long(argc, argv, "he:d:", long_options, &option_index); |
| 1192 | if (c == -1) { |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | switch (c) { |
| 1197 | case 'h': |
| 1198 | cmd_feature_help(); |
| 1199 | ret = 0; |
| 1200 | goto cleanup; |
| 1201 | case 'e': |
| 1202 | if (task) { |
| 1203 | fprintf(stderr, "Only one of enable or disable can be specified.\n"); |
| 1204 | goto cleanup; |
| 1205 | } |
| 1206 | task = 1; |
| 1207 | feat_names = optarg; |
| 1208 | break; |
| 1209 | case 'd': |
| 1210 | if (task) { |
| 1211 | fprintf(stderr, "Only one of enable, or disable can be specified.\n"); |
| 1212 | goto cleanup; |
| 1213 | } |
| 1214 | task = 2; |
| 1215 | feat_names = optarg; |
| 1216 | break; |
| 1217 | case '?': |
| 1218 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 1219 | goto cleanup; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | /* module name */ |
| 1224 | if (optind == argc) { |
| 1225 | fprintf(stderr, "Missing the module name.\n"); |
| 1226 | goto cleanup; |
| 1227 | } |
| 1228 | |
| 1229 | revision = NULL; |
| 1230 | model_name = argv[optind]; |
| 1231 | if (strchr(model_name, '@')) { |
| 1232 | revision = strchr(model_name, '@'); |
| 1233 | revision[0] = '\0'; |
| 1234 | ++revision; |
| 1235 | } |
| 1236 | |
| 1237 | module = ly_ctx_get_module(ctx, model_name, revision); |
| 1238 | #if 0 |
| 1239 | if (!module) { |
| 1240 | /* not a module, try to find it as a submodule */ |
| 1241 | module = (const struct lys_module *)ly_ctx_get_submodule(ctx, NULL, NULL, model_name, revision); |
| 1242 | } |
| 1243 | #endif |
| 1244 | |
| 1245 | if (module == NULL) { |
| 1246 | if (revision) { |
| 1247 | fprintf(stderr, "No (sub)module \"%s\" in revision %s found.\n", model_name, revision); |
| 1248 | } else { |
| 1249 | fprintf(stderr, "No (sub)module \"%s\" found.\n", model_name); |
| 1250 | } |
| 1251 | goto cleanup; |
| 1252 | } |
| 1253 | |
| 1254 | if (!task) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1255 | size_t len, max_len = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1256 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1257 | struct lysc_feature *features; |
| 1258 | |
| 1259 | printf("%s features:\n", module->name); |
| 1260 | |
| 1261 | if (module->compiled) { |
| 1262 | features = module->compiled->features; |
| 1263 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1264 | features = module->dis_features; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* get the max len */ |
| 1268 | LY_ARRAY_FOR(features, u) { |
| 1269 | len = strlen(features[u].name); |
| 1270 | if (len > max_len) { |
| 1271 | max_len = len; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | LY_ARRAY_FOR(features, u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1276 | printf("\t%-*s (%s)\n", (int)max_len, features[u].name, (features[u].flags & LYS_FENABLED) ? "on" : "off"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1277 | } |
| 1278 | if (!u) { |
| 1279 | printf("\t(none)\n"); |
| 1280 | } |
| 1281 | } else { |
| 1282 | feat_names = strtok(feat_names, ","); |
| 1283 | while (feat_names) { |
| 1284 | if (((task == 1) && lys_feature_enable(module, feat_names)) |
| 1285 | || ((task == 2) && lys_feature_disable(module, feat_names))) { |
| 1286 | fprintf(stderr, "Feature \"%s\" not found.\n", feat_names); |
| 1287 | ret = 1; |
| 1288 | } |
| 1289 | feat_names = strtok(NULL, ","); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | cleanup: |
| 1294 | free(*argv); |
| 1295 | free(argv); |
| 1296 | |
| 1297 | return ret; |
| 1298 | } |
| 1299 | |
| 1300 | int |
| 1301 | cmd_searchpath(const char *arg) |
| 1302 | { |
| 1303 | const char *path; |
| 1304 | const char * const *searchpaths; |
| 1305 | int index; |
| 1306 | struct stat st; |
| 1307 | |
| 1308 | for (path = strchr(arg, ' '); path && (path[0] == ' '); ++path); |
| 1309 | if (!path || (path[0] == '\0')) { |
| 1310 | searchpaths = ly_ctx_get_searchdirs(ctx); |
| 1311 | if (searchpaths) { |
| 1312 | for (index = 0; searchpaths[index]; index++) { |
| 1313 | fprintf(stdout, "%s\n", searchpaths[index]); |
| 1314 | } |
| 1315 | } |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | if ((!strncmp(path, "-h", 2) && (path[2] == '\0' || path[2] == ' ')) || |
| 1320 | (!strncmp(path, "--help", 6) && (path[6] == '\0' || path[6] == ' '))) { |
| 1321 | cmd_searchpath_help(); |
| 1322 | return 0; |
| 1323 | } else if (!strncmp(path, "--clear", 7) && (path[7] == '\0' || path[7] == ' ')) { |
| 1324 | ly_ctx_unset_searchdirs(ctx, NULL); |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | if (stat(path, &st) == -1) { |
| 1329 | fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno)); |
| 1330 | return 1; |
| 1331 | } |
| 1332 | if (!S_ISDIR(st.st_mode)) { |
| 1333 | fprintf(stderr, "\"%s\" is not a directory.\n", path); |
| 1334 | return 1; |
| 1335 | } |
| 1336 | |
| 1337 | ly_ctx_set_searchdir(ctx, path); |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | int |
| 1343 | cmd_clear(const char *arg) |
| 1344 | { |
| 1345 | struct ly_ctx *ctx_new; |
| 1346 | int options = 0; |
| 1347 | #if 0 |
| 1348 | int i; |
| 1349 | char *ylpath; |
| 1350 | const char * const *searchpaths; |
| 1351 | LYD_FORMAT format; |
| 1352 | |
| 1353 | /* get optional yang library file name */ |
| 1354 | for (i = 5; arg[i] && isspace(arg[i]); i++); |
| 1355 | if (arg[i]) { |
| 1356 | if (arg[i] == '-' && arg[i + 1] == 'e') { |
| 1357 | options = LY_CTX_NOYANGLIBRARY; |
| 1358 | goto create_empty; |
| 1359 | } else { |
| 1360 | ylpath = strdup(&arg[i]); |
| 1361 | format = detect_data_format(ylpath); |
| 1362 | if (format == LYD_UNKNOWN) { |
| 1363 | free(ylpath); |
| 1364 | fprintf(stderr, "Unable to resolve format of the yang library file, please add \".xml\" or \".json\" suffix.\n"); |
| 1365 | goto create_empty; |
| 1366 | } |
| 1367 | searchpaths = ly_ctx_get_searchdirs(ctx); |
| 1368 | ctx_new = ly_ctx_new_ylpath(searchpaths ? searchpaths[0] : NULL, ylpath, format, 0); |
| 1369 | free(ylpath); |
| 1370 | } |
| 1371 | } else { |
| 1372 | create_empty: |
| 1373 | #else |
Radek Krejci | 92f5ce9 | 2019-09-06 16:25:43 +0200 | [diff] [blame] | 1374 | (void) arg; /* TODO yang-library support */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1375 | { |
| 1376 | #endif |
| 1377 | ly_ctx_new(NULL, options, &ctx_new); |
| 1378 | } |
| 1379 | |
| 1380 | if (!ctx_new) { |
| 1381 | fprintf(stderr, "Failed to create context.\n"); |
| 1382 | return 1; |
| 1383 | } |
| 1384 | |
| 1385 | /* final switch */ |
| 1386 | ly_ctx_destroy(ctx, NULL); |
| 1387 | ctx = ctx_new; |
| 1388 | |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | int |
| 1393 | cmd_verb(const char *arg) |
| 1394 | { |
| 1395 | const char *verb; |
| 1396 | if (strlen(arg) < 5) { |
| 1397 | cmd_verb_help(); |
| 1398 | return 1; |
| 1399 | } |
| 1400 | |
| 1401 | verb = arg + 5; |
| 1402 | if (!strcmp(verb, "error") || !strcmp(verb, "0")) { |
| 1403 | ly_verb(LY_LLERR); |
| 1404 | #ifndef NDEBUG |
| 1405 | ly_verb_dbg(0); |
| 1406 | #endif |
| 1407 | } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) { |
| 1408 | ly_verb(LY_LLWRN); |
| 1409 | #ifndef NDEBUG |
| 1410 | ly_verb_dbg(0); |
| 1411 | #endif |
| 1412 | } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) { |
| 1413 | ly_verb(LY_LLVRB); |
| 1414 | #ifndef NDEBUG |
| 1415 | ly_verb_dbg(0); |
| 1416 | #endif |
| 1417 | } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) { |
| 1418 | ly_verb(LY_LLDBG); |
| 1419 | #ifndef NDEBUG |
| 1420 | ly_verb_dbg(LY_LDGDICT | LY_LDGYANG | LY_LDGYIN | LY_LDGXPATH | LY_LDGDIFF); |
| 1421 | #endif |
| 1422 | } else { |
| 1423 | fprintf(stderr, "Unknown verbosity \"%s\"\n", verb); |
| 1424 | return 1; |
| 1425 | } |
| 1426 | |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | #ifndef NDEBUG |
| 1431 | |
| 1432 | int |
| 1433 | cmd_debug(const char *arg) |
| 1434 | { |
| 1435 | const char *beg, *end; |
| 1436 | int grps = 0; |
| 1437 | if (strlen(arg) < 6) { |
| 1438 | cmd_debug_help(); |
| 1439 | return 1; |
| 1440 | } |
| 1441 | |
| 1442 | end = arg + 6; |
| 1443 | while (end[0]) { |
| 1444 | for (beg = end; isspace(beg[0]); ++beg); |
| 1445 | if (!beg[0]) { |
| 1446 | break; |
| 1447 | } |
| 1448 | |
| 1449 | for (end = beg; (end[0] && !isspace(end[0])); ++end); |
| 1450 | |
| 1451 | if (!strncmp(beg, "dict", end - beg)) { |
| 1452 | grps |= LY_LDGDICT; |
| 1453 | } else if (!strncmp(beg, "yang", end - beg)) { |
| 1454 | grps |= LY_LDGYANG; |
| 1455 | } else if (!strncmp(beg, "yin", end - beg)) { |
| 1456 | grps |= LY_LDGYIN; |
| 1457 | } else if (!strncmp(beg, "xpath", end - beg)) { |
| 1458 | grps |= LY_LDGXPATH; |
| 1459 | } else if (!strncmp(beg, "diff", end - beg)) { |
| 1460 | grps |= LY_LDGDIFF; |
| 1461 | } else { |
| 1462 | fprintf(stderr, "Unknown debug group \"%.*s\"\n", (int)(end - beg), beg); |
| 1463 | return 1; |
| 1464 | } |
| 1465 | } |
| 1466 | ly_verb_dbg(grps); |
| 1467 | |
| 1468 | return 0; |
| 1469 | } |
| 1470 | |
| 1471 | #endif |
| 1472 | |
| 1473 | int |
| 1474 | cmd_quit(const char *UNUSED(arg)) |
| 1475 | { |
| 1476 | done = 1; |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | int |
| 1481 | cmd_help(const char *arg) |
| 1482 | { |
| 1483 | int i; |
| 1484 | char *args = strdup(arg); |
| 1485 | char *cmd = NULL; |
| 1486 | |
| 1487 | strtok(args, " "); |
| 1488 | if ((cmd = strtok(NULL, " ")) == NULL) { |
| 1489 | |
| 1490 | generic_help: |
| 1491 | fprintf(stdout, "Available commands:\n"); |
| 1492 | |
| 1493 | for (i = 0; commands[i].name; i++) { |
| 1494 | if (commands[i].helpstring != NULL) { |
| 1495 | fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring); |
| 1496 | } |
| 1497 | } |
| 1498 | } else { |
| 1499 | /* print specific help for the selected command */ |
| 1500 | |
| 1501 | /* get the command of the specified name */ |
| 1502 | for (i = 0; commands[i].name; i++) { |
| 1503 | if (strcmp(cmd, commands[i].name) == 0) { |
| 1504 | break; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /* execute the command's help if any valid command specified */ |
| 1509 | if (commands[i].name) { |
| 1510 | if (commands[i].help_func != NULL) { |
| 1511 | commands[i].help_func(); |
| 1512 | } else { |
| 1513 | printf("%s\n", commands[i].helpstring); |
| 1514 | } |
| 1515 | } else { |
| 1516 | /* if unknown command specified, print the list of commands */ |
| 1517 | printf("Unknown command \'%s\'\n", cmd); |
| 1518 | goto generic_help; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | free(args); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | COMMAND commands[] = { |
| 1527 | {"help", cmd_help, NULL, "Display commands description"}, |
| 1528 | {"add", cmd_add, cmd_add_help, "Add a new model from a specific file"}, |
| 1529 | {"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"}, |
| 1530 | {"print", cmd_print, cmd_print_help, "Print a model"}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1531 | {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"}, |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1532 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1533 | {"xpath", cmd_xpath, cmd_xpath_help, "Get data nodes satisfying an XPath expression"}, |
| 1534 | {"list", cmd_list, cmd_list_help, "List all the loaded models"}, |
| 1535 | #endif |
| 1536 | {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"}, |
| 1537 | {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"}, |
| 1538 | {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"}, |
| 1539 | {"verb", cmd_verb, cmd_verb_help, "Change verbosity"}, |
| 1540 | #ifndef NDEBUG |
| 1541 | {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"}, |
| 1542 | #endif |
| 1543 | {"quit", cmd_quit, NULL, "Quit the program"}, |
| 1544 | /* synonyms for previous commands */ |
| 1545 | {"?", cmd_help, NULL, "Display commands description"}, |
| 1546 | {"exit", cmd_quit, NULL, "Quit the program"}, |
| 1547 | {NULL, NULL, NULL, NULL} |
| 1548 | }; |