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