Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main_ni.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool - noninteractive code |
| 5 | * |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 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 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <getopt.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 22 | #include <string.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 23 | #include <strings.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 24 | #include <sys/stat.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 25 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 26 | #include "libyang.h" |
| 27 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 28 | #include "common.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "tools/config.h" |
| 30 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 31 | /** |
| 32 | * @brief Context structure to hold and pass variables in a structured form. |
| 33 | */ |
| 34 | struct context { |
| 35 | /* libyang context for the run */ |
| 36 | struct ly_ctx *ctx; |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 38 | /* prepared output (--output option or stdout by default) */ |
| 39 | struct ly_out *out; |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 40 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 41 | struct ly_set searchpaths; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 42 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 43 | /* options flags */ |
| 44 | uint8_t list; /* -l option to print list of schemas */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 45 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 46 | /* |
| 47 | * schema |
| 48 | */ |
| 49 | /* set schema modules' features via --feature option (struct schema_features *) */ |
| 50 | struct ly_set schema_features; |
| 51 | |
| 52 | /* set of loaded schema modules (struct lys_module *) */ |
| 53 | struct ly_set schema_modules; |
| 54 | |
| 55 | /* options to parse and print schema modules */ |
| 56 | uint32_t schema_parse_options; |
| 57 | uint32_t schema_print_options; |
| 58 | |
| 59 | /* specification of printing schema node subtree, option --schema-node */ |
| 60 | const char *schema_node_path; |
| 61 | const struct lysc_node *schema_node; |
| 62 | |
| 63 | /* value of --format in case of schema format */ |
| 64 | LYS_OUTFORMAT schema_out_format; |
| 65 | |
| 66 | /* |
| 67 | * data |
| 68 | */ |
| 69 | /* various options based on --type option */ |
| 70 | uint8_t data_type; /* values taken from LYD_VALIDATE_OP and extended by 0 for standard data tree */ |
| 71 | uint32_t data_parse_options; |
| 72 | uint32_t data_validate_options; |
| 73 | uint32_t data_print_options; |
| 74 | |
| 75 | /* flag for --merge option */ |
| 76 | uint8_t data_merge; |
| 77 | |
| 78 | /* value of --format in case of data format */ |
| 79 | LYD_FORMAT data_out_format; |
| 80 | |
| 81 | /* input data files (struct cmdline_file *) */ |
| 82 | struct ly_set data_inputs; |
| 83 | |
| 84 | /* the request files for reply data (struct cmdline_file *) |
| 85 | * In case the data_type is PARSE_REPLY, the parsing function requires information about the request for this reply. |
| 86 | * One way to provide the request is a data file containing the full RPC/Action request which will be parsed. |
| 87 | * Alternatively, it can be set as the Path of the requested RPC/Action and in that case it is stored in |
| 88 | * data_request_paths. |
| 89 | */ |
| 90 | struct ly_set data_requests; |
| 91 | /* An alternative way of providing requests for parsing data replies, instead of providing full |
| 92 | * request in a data file, only the Path of the requested RPC/Action is provided and stored as |
| 93 | * const char *. Note that the number of items in the set must be 1 (1 applies to all) or equal to |
| 94 | * data_inputs_count (1 to 1 mapping). |
| 95 | */ |
| 96 | struct ly_set data_request_paths; |
| 97 | |
| 98 | /* storage for --operational */ |
| 99 | struct cmdline_file data_operational; |
| 100 | }; |
| 101 | |
| 102 | static void |
| 103 | erase_context(struct context *c) |
| 104 | { |
| 105 | /* data */ |
| 106 | ly_set_erase(&c->data_inputs, free_cmdline_file); |
| 107 | ly_set_erase(&c->data_requests, free_cmdline_file); |
| 108 | ly_set_erase(&c->data_request_paths, NULL); |
| 109 | ly_in_free(c->data_operational.in, 1); |
| 110 | |
| 111 | /* schema */ |
| 112 | ly_set_erase(&c->schema_features, free_features); |
| 113 | ly_set_erase(&c->schema_modules, NULL); |
| 114 | |
| 115 | /* context */ |
| 116 | ly_set_erase(&c->searchpaths, NULL); |
| 117 | |
| 118 | ly_out_free(c->out, NULL, 0); |
| 119 | ly_ctx_destroy(c->ctx, NULL); |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | version(void) |
| 124 | { |
| 125 | printf("yanglint %s\n", PROJECT_VERSION); |
| 126 | } |
| 127 | |
| 128 | static void |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 129 | help(int shortout) |
| 130 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 131 | |
| 132 | printf("Usage:\n" |
| 133 | " yanglint [Options] [-f { yang | yin | info}] <schema>...\n" |
| 134 | " Validates the YANG module in <schema>, and all its dependencies.\n\n" |
| 135 | " yanglint [Options] [-f { xml | json }] <schema>... <file> [<request>]...\n" |
| 136 | " Validates the YANG modeled data in <file> according to the <schema>.\n\n" |
| 137 | " yanglint\n" |
| 138 | " Starts interactive mode with more features.\n\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 139 | |
| 140 | if (shortout) { |
| 141 | return; |
| 142 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 143 | printf("Options:\n" |
| 144 | " -h, --help Show this help message and exit.\n" |
| 145 | " -v, --version Show version number and exit.\n" |
| 146 | " -V, --verbose Show verbose messages, can be used multiple times to\n" |
| 147 | " increase verbosity.\n" |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 148 | #ifndef NDEBUG |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 149 | " -G GROUPS, --debug=GROUPS\n" |
| 150 | " Enable printing of specific debugging message group\n" |
| 151 | " (nothing will be printed unless verbosity is set to debug):\n" |
| 152 | " <group>[,<group>]* (dict, xpath)\n\n" |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 153 | #endif |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 154 | |
| 155 | " -d MODE, --default=MODE\n" |
| 156 | " Print data with default values, according to the MODE\n" |
| 157 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 158 | " must be loaded):\n" |
| 159 | " all - Add missing default nodes.\n" |
| 160 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 161 | " nodes with the attribute.\n" |
| 162 | " trim - Remove all nodes with a default value.\n" |
| 163 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n" |
| 164 | |
| 165 | " -D, --disable-searchdir\n" |
| 166 | " Do not implicitly search in current working directory for\n" |
| 167 | " schema modules. If specified a second time, do not even\n" |
| 168 | " search in the module directory (all modules must be \n" |
| 169 | " explicitly specified).\n\n" |
| 170 | |
| 171 | " -p PATH, --path=PATH\n" |
| 172 | " Search path for schema (YANG/YIN) modules. The option can be\n" |
| 173 | " used multiple times. The current working directory and the\n" |
| 174 | " path of the module being added is used implicitly.\n\n" |
| 175 | |
| 176 | " -F FEATURES, --features=FEATURES\n" |
| 177 | " Features to support, default all.\n" |
| 178 | " <modname>:[<feature>,]*\n\n" |
| 179 | |
| 180 | " -i, --makeimplemented\n" |
| 181 | " Make the imported modules \"referenced\" from any loaded\n" |
| 182 | " module also implemented. If specified a second time, all the\n" |
| 183 | " modules are set implemented.\n\n" |
| 184 | |
| 185 | " -l, --list Print info about the loaded schemas.\n" |
| 186 | " (i - imported module, I - implemented module)\n" |
| 187 | " In case the -f option with data encoding is specified,\n" |
| 188 | " the list is printed as ietf-yang-library data.\n\n" |
| 189 | |
| 190 | " -o OUTFILE, --output=OUTFILE\n" |
| 191 | " Write the output to OUTFILE instead of stdout.\n\n" |
| 192 | |
| 193 | " -f FORMAT, --format=FORMAT\n" |
| 194 | " Convert input into FORMAT. Supported formats: \n" |
| 195 | " yang, yin, tree and info for schemas,\n" |
| 196 | " xml, json for data.\n\n" |
| 197 | |
| 198 | " -P PATH, --schema-node=PATH\n" |
| 199 | " Print only the specified subtree of the schema.\n" |
| 200 | " The PATH is the XPath subset mentioned in documentation as\n" |
| 201 | " the Path format. The option can be combined with --single-node\n" |
| 202 | " option to print information only about the specified node.\n" |
| 203 | " -q, --single-node\n" |
| 204 | " Supplement to the --schema-node option to print information\n" |
| 205 | " only about a single node specified as PATH argument.\n\n" |
| 206 | |
| 207 | " -s, --strict Strict data parsing (do not skip unknown data), has no effect\n" |
| 208 | " for schemas.\n\n" |
| 209 | |
| 210 | " -e, --present Validate only with the schema modules whose data actually\n" |
| 211 | " exist in the provided input data files. Takes effect only\n" |
| 212 | " with the 'data' or 'config' TYPEs. Used to avoid requiring\n" |
| 213 | " mandatory nodes from modules which data are not present in the\n" |
| 214 | " provided input data files.\n\n" |
| 215 | |
| 216 | " -t TYPE, --type=TYPE\n" |
| 217 | " Specify data tree type in the input data file(s):\n" |
| 218 | " data - Complete datastore with status data (default type).\n" |
| 219 | " config - Configuration datastore (without status data).\n" |
| 220 | " get - Result of the NETCONF <get> operation.\n" |
| 221 | " getconfig - Result of the NETCONF <get-config> operation.\n" |
| 222 | " edit - Content of the NETCONF <edit-config> operation.\n" |
| 223 | " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n" |
| 224 | " RPC/Action input statement.\n" |
| 225 | " reply - Reply to the RPC/Action. Besides the reply itself,\n" |
| 226 | " yanglint(1) requires information about the request for\n" |
| 227 | " the reply. The request (RPC/Action) can be provided as\n" |
| 228 | " the --request option or as another input data <file>\n" |
| 229 | " provided right after the reply data <file> and\n" |
| 230 | " containing complete RPC/Action for the reply.\n" |
| 231 | " notif - Notification instance (content of the <notification>\n" |
| 232 | " element without <eventTime>).\n" |
| 233 | " -r PATH, --request=PATH\n" |
| 234 | " The alternative way of providing request information for the\n" |
| 235 | " '--type=reply'. The PATH is the XPath subset described in\n" |
| 236 | " documentation as Path format. It is required to point to the\n" |
| 237 | " RPC or Action in the schema which is supposed to be a request\n" |
| 238 | " for the reply(ies) being parsed from the input data files.\n" |
| 239 | " In case of multiple input data files, the 'request' option can\n" |
| 240 | " be set once for all the replies or multiple times each for the\n" |
| 241 | " respective input data file.\n\n" |
| 242 | |
| 243 | " -O FILE, --operational=FILE\n" |
| 244 | " Provide optional data to extend validation of the 'rpc',\n" |
| 245 | " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n" |
| 246 | " the :running configuration datastore and state data\n" |
| 247 | " (operational datastore) referenced from the RPC/Notification.\n\n" |
| 248 | |
| 249 | " -m, --merge Merge input data files into a single tree and validate at\n" |
| 250 | " once.The option has effect only for 'data' and 'config' TYPEs.\n\n" |
| 251 | |
| 252 | #if 0 |
| 253 | " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n" |
| 254 | "Tree output specific options:\n" |
| 255 | " --tree-help - Print help on tree symbols and exit.\n" |
| 256 | " --tree-print-groupings\n" |
| 257 | " Print top-level groupings in a separate section.\n" |
| 258 | " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n" |
| 259 | " --tree-no-leafref-target\n" |
| 260 | " Do not print target nodes of leafrefs.\n" |
| 261 | " --tree-path=SCHEMA_PATH\n" |
| 262 | " Print only the specified subtree.\n" |
| 263 | " --tree-line-length=LINE_LENGTH\n" |
| 264 | " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n" |
| 265 | " can often appear).\n\n" |
| 266 | #endif |
| 267 | "\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 270 | static void |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 271 | libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 272 | { |
| 273 | char *levstr; |
| 274 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 275 | switch (level) { |
| 276 | case LY_LLERR: |
| 277 | levstr = "err :"; |
| 278 | break; |
| 279 | case LY_LLWRN: |
| 280 | levstr = "warn:"; |
| 281 | break; |
| 282 | case LY_LLVRB: |
| 283 | levstr = "verb:"; |
| 284 | break; |
| 285 | default: |
| 286 | levstr = "dbg :"; |
| 287 | break; |
| 288 | } |
| 289 | if (path) { |
| 290 | fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path); |
| 291 | } else { |
| 292 | fprintf(stderr, "libyang %s %s\n", levstr, msg); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 296 | static int |
| 297 | fill_context_inputs(int argc, char *argv[], struct context *c) |
| 298 | { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 299 | struct ly_in *in = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 300 | uint8_t request_expected = 0; |
| 301 | |
| 302 | /* process the operational content if any */ |
| 303 | if (c->data_operational.path) { |
| 304 | if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) { |
| 305 | return -1; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | for (int i = 0; i < argc - optind; i++) { |
| 310 | LYS_INFORMAT format_schema = LYS_IN_UNKNOWN; |
| 311 | LYD_FORMAT format_data = LYD_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 312 | |
| 313 | if (get_input(argv[optind + i], &format_schema, &format_data, &in)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 314 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (format_schema) { |
| 318 | LY_ERR ret; |
| 319 | uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */ |
| 320 | char *dir, *module; |
| 321 | const char *fall = "*"; |
| 322 | const char **features = &fall; |
| 323 | const struct lys_module *mod; |
| 324 | |
| 325 | if (parse_schema_path(argv[optind + i], &dir, &module)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 326 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* add temporarily also the path of the module itself */ |
| 330 | if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) { |
| 331 | path_unset = 0; |
| 332 | } |
| 333 | |
| 334 | /* get features list for this module */ |
| 335 | get_features(&c->schema_features, module, &features); |
| 336 | |
| 337 | /* temporary cleanup */ |
| 338 | free(dir); |
| 339 | free(module); |
| 340 | |
| 341 | ret = lys_parse(c->ctx, in, format_schema, features, &mod); |
| 342 | ly_ctx_unset_searchdir_last(c->ctx, path_unset); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 343 | if (ret) { |
| 344 | YLMSG_E("Processing schema module from %s failed.\n", argv[optind + i]); |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 345 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (c->schema_out_format) { |
| 349 | /* modules will be printed */ |
| 350 | if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) { |
| 351 | YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]); |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 352 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } else if (request_expected) { |
| 356 | if (fill_cmdline_file(&c->data_requests, in, argv[optind + i], format_data)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 357 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | request_expected = 0; |
| 361 | } else if (format_data) { |
| 362 | struct cmdline_file *rec; |
| 363 | |
| 364 | rec = fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data); |
| 365 | if (!rec) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 366 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if ((c->data_type == LYD_VALIDATE_OP_REPLY) && !c->data_request_paths.count) { |
| 370 | /* requests for the replies are expected in another input file */ |
| 371 | if (++i == argc - optind) { |
| 372 | /* there is no such file */ |
| 373 | YLMSG_E("Missing request input file for the reply input file %s.\n", rec->path); |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 374 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | request_expected = 1; |
| 378 | } |
| 379 | } |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 380 | |
| 381 | ly_in_free(in, 1); |
| 382 | in = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (request_expected) { |
| 386 | YLMSG_E("Missing request input file for the reply input file %s.\n", |
| 387 | ((struct cmdline_file *)c->data_inputs.objs[c->data_inputs.count - 1])->path); |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | return 0; |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame^] | 392 | |
| 393 | error: |
| 394 | ly_in_free(in, 1); |
| 395 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @brief Process command line options and store the settings into the context. |
| 400 | * |
| 401 | * return -1 in case of error; |
| 402 | * return 0 in case of success and ready to process |
| 403 | * return 1 in case of success, but expect to exit. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 404 | */ |
| 405 | static int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 406 | fill_context(int argc, char *argv[], struct context *c) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 407 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 408 | int ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 409 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 410 | int opt, opt_index; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 411 | struct option options[] = { |
Radek Krejci | ff61c88 | 2020-09-03 13:04:18 +0200 | [diff] [blame] | 412 | {"default", required_argument, NULL, 'd'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 413 | {"disable-searchdir", no_argument, NULL, 'D'}, |
| 414 | {"present", no_argument, NULL, 'e'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 415 | {"format", required_argument, NULL, 'f'}, |
| 416 | {"features", required_argument, NULL, 'F'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 417 | #ifndef NDEBUG |
| 418 | {"debug", required_argument, NULL, 'G'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 419 | #endif |
| 420 | {"help", no_argument, NULL, 'h'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 421 | {"makeimplemented", no_argument, NULL, 'i'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 422 | {"list", no_argument, NULL, 'l'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 423 | {"merge", no_argument, NULL, 'm'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 424 | {"output", required_argument, NULL, 'o'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 425 | {"operational", required_argument, NULL, 'O'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 426 | {"path", required_argument, NULL, 'p'}, |
| 427 | {"schema-node", required_argument, NULL, 'P'}, |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 428 | {"single-node", no_argument, NULL, 'q'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 429 | {"request", required_argument, NULL, 'r'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 430 | {"strict", no_argument, NULL, 's'}, |
| 431 | {"type", required_argument, NULL, 't'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 432 | {"version", no_argument, NULL, 'v'}, |
| 433 | {"verbose", no_argument, NULL, 'V'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 434 | {NULL, 0, NULL, 0} |
| 435 | }; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 436 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 437 | uint16_t options_ctx = 0; |
| 438 | uint8_t data_type_set = 0; |
| 439 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 440 | #ifndef NDEBUG |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 441 | while ((opt = getopt_long(argc, argv, "d:Df:F:hilmo:P:qr:st:vV", options, &opt_index)) != -1) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 442 | #else |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 443 | while ((opt = getopt_long(argc, argv, "d:Df:F:G:hilmo:P:qr:st:vV", options, &opt_index)) != -1) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 444 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 445 | switch (opt) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 446 | case 'd': /* --default */ |
| 447 | if (!strcasecmp(optarg, "all")) { |
| 448 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL; |
| 449 | } else if (!strcasecmp(optarg, "all-tagged")) { |
| 450 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG; |
| 451 | } else if (!strcasecmp(optarg, "trim")) { |
| 452 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM; |
| 453 | } else if (!strcasecmp(optarg, "implicit-tagged")) { |
| 454 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 455 | } else { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 456 | YLMSG_E("Unknown default mode %s\n", optarg); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 457 | help(1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 458 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 459 | } |
| 460 | break; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 461 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 462 | case 'D': /* --disable-search */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 463 | if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 464 | YLMSG_W("The -D option specified too many times.\n"); |
| 465 | } |
| 466 | if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 467 | options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 468 | options_ctx |= LY_CTX_DISABLE_SEARCHDIRS; |
| 469 | } else { |
| 470 | options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 471 | } |
| 472 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 473 | |
| 474 | case 'p': { /* --path */ |
| 475 | struct stat st; |
| 476 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 477 | if (stat(optarg, &st) == -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 478 | YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno)); |
| 479 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 480 | } |
| 481 | if (!S_ISDIR(st.st_mode)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 482 | YLMSG_E("Provided search path is not a directory.\n"); |
| 483 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 484 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 485 | |
| 486 | if (ly_set_add(&c->searchpaths, optarg, 0, NULL)) { |
| 487 | YLMSG_E("Storing searchpath failed.\n"); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | break; |
| 492 | } /* case 'p' */ |
| 493 | |
| 494 | case 'i': /* --makeimplemented */ |
| 495 | if (options_ctx & LY_CTX_REF_IMPLEMENTED) { |
| 496 | options_ctx &= ~LY_CTX_REF_IMPLEMENTED; |
| 497 | options_ctx |= LY_CTX_ALL_IMPLEMENTED; |
| 498 | } else { |
| 499 | options_ctx |= LY_CTX_REF_IMPLEMENTED; |
| 500 | } |
| 501 | break; |
| 502 | |
| 503 | case 'F': /* --features */ |
| 504 | if (parse_features(optarg, &c->schema_features)) { |
| 505 | return -1; |
| 506 | } |
| 507 | break; |
| 508 | |
| 509 | case 'l': /* --list */ |
| 510 | c->list = 1; |
| 511 | break; |
| 512 | |
| 513 | case 'o': /* --output */ |
| 514 | if (c->out) { |
| 515 | YLMSG_E("Only a single output can be specified.\n"); |
| 516 | return -1; |
| 517 | } else { |
| 518 | if (ly_out_new_filepath(optarg, &c->out)) { |
| 519 | YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 520 | return -1; |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 521 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 522 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 523 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 524 | |
| 525 | case 'f': /* --format */ |
| 526 | if (!strcasecmp(optarg, "yang")) { |
| 527 | c->schema_out_format = LYS_OUT_YANG; |
| 528 | c->data_out_format = 0; |
| 529 | } else if (!strcasecmp(optarg, "yin")) { |
| 530 | c->schema_out_format = LYS_OUT_YIN; |
| 531 | c->data_out_format = 0; |
| 532 | } else if (!strcasecmp(optarg, "info")) { |
| 533 | c->schema_out_format = LYS_OUT_YANG_COMPILED; |
| 534 | c->data_out_format = 0; |
| 535 | } else if (!strcasecmp(optarg, "tree")) { |
| 536 | c->schema_out_format = LYS_OUT_TREE; |
| 537 | c->data_out_format = 0; |
| 538 | } else if (!strcasecmp(optarg, "xml")) { |
| 539 | c->schema_out_format = 0; |
| 540 | c->data_out_format = LYD_XML; |
| 541 | } else if (!strcasecmp(optarg, "json")) { |
| 542 | c->schema_out_format = 0; |
| 543 | c->data_out_format = LYD_JSON; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 544 | } else { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 545 | YLMSG_E("Unknown output format %s\n", optarg); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 546 | help(1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 547 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 548 | } |
| 549 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 550 | |
| 551 | case 'P': /* --schema-node */ |
| 552 | c->schema_node_path = optarg; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 553 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 554 | |
| 555 | case 'q': /* --single-node */ |
| 556 | c->schema_print_options |= LYS_PRINT_NO_SUBSTMT; |
| 557 | break; |
| 558 | |
| 559 | case 's': /* --strict */ |
| 560 | c->data_parse_options |= LYD_PARSE_STRICT; |
| 561 | break; |
| 562 | |
| 563 | case 'e': /* --present */ |
| 564 | c->data_validate_options |= LYD_VALIDATE_PRESENT; |
| 565 | break; |
| 566 | |
| 567 | case 't': /* --type */ |
| 568 | if (data_type_set) { |
| 569 | YLMSG_E("The data type (-t) cannot be set multiple times.\n"); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | if (!strcasecmp(optarg, "config")) { |
| 574 | c->data_parse_options |= LYD_PARSE_NO_STATE; |
| 575 | } else if (!strcasecmp(optarg, "get")) { |
| 576 | c->data_parse_options |= LYD_PARSE_ONLY; |
| 577 | } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config")) { |
| 578 | c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE; |
| 579 | } else if (!strcasecmp(optarg, "edit")) { |
| 580 | c->data_parse_options |= LYD_PARSE_ONLY; |
| 581 | } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) { |
| 582 | c->data_type = LYD_VALIDATE_OP_RPC; |
| 583 | } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) { |
| 584 | c->data_type = LYD_VALIDATE_OP_REPLY; |
| 585 | } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) { |
| 586 | c->data_type = LYD_VALIDATE_OP_NOTIF; |
| 587 | } else if (!strcasecmp(optarg, "data")) { |
| 588 | /* default option */ |
| 589 | } else { |
| 590 | YLMSG_E("Unknown data tree type %s\n", optarg); |
| 591 | help(1); |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | data_type_set = 1; |
| 596 | break; |
| 597 | |
| 598 | case 'O': /* --operational */ |
| 599 | if (c->data_operational.path) { |
| 600 | YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n"); |
| 601 | return -1; |
| 602 | } |
| 603 | c->data_operational.path = optarg; |
| 604 | break; |
| 605 | |
| 606 | case 'r': /* --request */ |
| 607 | if (ly_set_add(&c->data_request_paths, optarg, 0, NULL)) { |
| 608 | YLMSG_E("Storing request path failed.\n"); |
| 609 | return -1; |
| 610 | } |
| 611 | break; |
| 612 | |
| 613 | case 'm': /* --merge */ |
| 614 | c->data_merge = 1; |
| 615 | break; |
| 616 | |
| 617 | case 'h': /* --help */ |
| 618 | help(0); |
| 619 | return 1; |
| 620 | |
| 621 | case 'v': /* --version */ |
| 622 | version(); |
| 623 | return 1; |
| 624 | |
| 625 | case 'V': { /* --verbose */ |
| 626 | LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR); |
| 627 | ly_log_level(verbosity); |
| 628 | |
| 629 | if (verbosity < LY_LLDBG) { |
| 630 | ly_log_level(verbosity + 1); |
| 631 | } |
| 632 | break; |
| 633 | } /* case 'V' */ |
| 634 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 635 | #ifndef NDEBUG |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 636 | case 'G': { /* --debug */ |
| 637 | uint32_t dbg_groups = 0; |
| 638 | const char *ptr = optarg; |
| 639 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 640 | while (ptr[0]) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 641 | if (!strncasecmp(ptr, "dict", 4)) { |
| 642 | dbg_groups |= LY_LDGDICT; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 643 | ptr += 4; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 644 | } else if (!strncasecmp(ptr, "xpath", 5)) { |
| 645 | dbg_groups |= LY_LDGXPATH; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 646 | ptr += 5; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | if (ptr[0]) { |
| 650 | if (ptr[0] != ',') { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 651 | YLMSG_E("Unknown debug group string \"%s\"\n", optarg); |
| 652 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 653 | } |
| 654 | ++ptr; |
| 655 | } |
| 656 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 657 | ly_log_dbg_groups(dbg_groups); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 658 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 659 | } /* case 'G' */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 660 | #endif |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 661 | } /* switch */ |
| 662 | } |
| 663 | |
| 664 | /* libyang context */ |
| 665 | if (ly_ctx_new(NULL, options_ctx, &c->ctx)) { |
| 666 | YLMSG_E("Unable to create libyang context.\n"); |
| 667 | return -1; |
| 668 | } |
| 669 | for (uint32_t u = 0; u < c->searchpaths.count; ++u) { |
| 670 | ly_ctx_set_searchdir(c->ctx, c->searchpaths.objs[u]); |
| 671 | } |
| 672 | |
| 673 | /* additional checks for the options combinations */ |
| 674 | if (!c->list && (optind >= argc)) { |
| 675 | help(1); |
| 676 | YLMSG_E("Missing <schema> to process.\n"); |
| 677 | return 1; |
| 678 | } |
| 679 | |
| 680 | if (c->data_merge) { |
| 681 | if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) { |
| 682 | /* switch off the option, incompatible input data type */ |
| 683 | c->data_merge = 0; |
| 684 | } else { |
| 685 | /* postpone validation after the merge of all the input data */ |
| 686 | c->data_parse_options |= LYD_PARSE_ONLY; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 690 | if (c->data_operational.path && !c->data_type) { |
| 691 | YLMSG_E("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n"); |
| 692 | c->data_operational.path = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 693 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 694 | |
| 695 | /* default output stream */ |
| 696 | if (!c->out) { |
| 697 | if (ly_out_new_file(stdout, &c->out)) { |
| 698 | YLMSG_E("Unable to set stdout as output.\n"); |
| 699 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 700 | } |
| 701 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 702 | |
| 703 | /* process input files provided as standalone command line arguments, |
| 704 | * schema modules are parsed and inserted into the context, |
| 705 | * data files are just checked and prepared into internal structures for further processing */ |
| 706 | ret = fill_context_inputs(argc, argv, c); |
| 707 | if (ret) { |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | /* the second batch of checks */ |
| 712 | if (c->schema_print_options && !c->schema_out_format) { |
| 713 | YLMSG_W("Schema printer options specified, but the schema output format is missing.\n"); |
| 714 | } |
| 715 | if (c->schema_parse_options && !c->schema_modules.count) { |
| 716 | YLMSG_W("Schema parser options specified, but no schema input file provided.\n"); |
| 717 | } |
| 718 | if (c->data_print_options && !c->data_out_format) { |
| 719 | YLMSG_W("data printer options specified, but the data output format is missing.\n"); |
| 720 | } |
| 721 | if ((c->data_parse_options || c->data_type) && !c->data_inputs.count) { |
| 722 | YLMSG_W("Data parser options specified, but no data input file provided.\n"); |
| 723 | } |
| 724 | |
| 725 | if (c->schema_node_path) { |
| 726 | c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0); |
| 727 | if (!c->schema_node) { |
| 728 | c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1); |
| 729 | |
| 730 | if (!c->schema_node) { |
| 731 | YLMSG_E("Invalid schema path.\n"); |
| 732 | return -1; |
| 733 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 734 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 735 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 736 | |
| 737 | if (c->data_type == LYD_VALIDATE_OP_REPLY) { |
| 738 | if (check_request_paths(c->ctx, &c->data_request_paths, &c->data_inputs)) { |
| 739 | return -1; |
| 740 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 741 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | int |
| 747 | main_ni(int argc, char *argv[]) |
| 748 | { |
| 749 | int ret = EXIT_SUCCESS, r; |
| 750 | struct context c = {0}; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 751 | |
| 752 | /* set callback for printing libyang messages */ |
| 753 | ly_set_log_clb(libyang_verbclb, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 754 | |
| 755 | r = fill_context(argc, argv, &c); |
| 756 | if (r < 0) { |
| 757 | ret = EXIT_FAILURE; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 758 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 759 | if (r) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 760 | goto cleanup; |
| 761 | } |
| 762 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 763 | /* do the required job - parse, validate, print */ |
| 764 | |
| 765 | if (c.list) { |
| 766 | /* print the list of schemas */ |
| 767 | print_list(c.out, c.ctx, c.data_out_format); |
| 768 | } else if (c.schema_out_format) { |
| 769 | if (c.schema_node) { |
| 770 | ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, 0, c.schema_print_options); |
| 771 | if (ret) { |
| 772 | YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path); |
| 773 | goto cleanup; |
| 774 | } |
| 775 | } else { |
| 776 | for (uint32_t u = 0; u < c.schema_modules.count; ++u) { |
| 777 | ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, 0, |
| 778 | c.schema_print_options); |
| 779 | if (ret) { |
| 780 | YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name); |
| 781 | goto cleanup; |
| 782 | } |
| 783 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 784 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 785 | } else if (c.data_out_format) { |
| 786 | if (process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out, |
| 787 | c.data_parse_options, c.data_validate_options, c.data_print_options, |
| 788 | &c.data_operational, &c.data_inputs, &c.data_request_paths, &c.data_requests, NULL)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 789 | goto cleanup; |
| 790 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 791 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 792 | |
| 793 | cleanup: |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 794 | /* cleanup */ |
| 795 | erase_context(&c); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 796 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 797 | return ret; |
| 798 | } |