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