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