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> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief libyang's yanglint tool - non-interactive code |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 7 | * Copyright (c) 2020 - 2022 CESNET, z.s.p.o. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 17 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <getopt.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 23 | #include <string.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 24 | #include <strings.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 26 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 27 | #include "libyang.h" |
| 28 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 29 | #include "common.h" |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 30 | #include "out.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "tools/config.h" |
| 32 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 33 | /** |
| 34 | * @brief Context structure to hold and pass variables in a structured form. |
| 35 | */ |
| 36 | struct context { |
| 37 | /* libyang context for the run */ |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 38 | const char *yang_lib_file; |
| 39 | uint16_t ctx_options; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 40 | struct ly_ctx *ctx; |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 42 | /* prepared output (--output option or stdout by default) */ |
| 43 | struct ly_out *out; |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 44 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 45 | char *searchpaths; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 46 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 47 | /* options flags */ |
| 48 | uint8_t list; /* -l option to print list of schemas */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 49 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 50 | /* line length for 'tree' format */ |
| 51 | size_t line_length; /* --tree-line-length */ |
| 52 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 53 | /* |
| 54 | * schema |
| 55 | */ |
Michal Vasko | a9a9861 | 2021-11-22 10:00:27 +0100 | [diff] [blame] | 56 | /* set schema modules' features via --features option (struct schema_features *) */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 57 | struct ly_set schema_features; |
| 58 | |
| 59 | /* set of loaded schema modules (struct lys_module *) */ |
| 60 | struct ly_set schema_modules; |
| 61 | |
| 62 | /* options to parse and print schema modules */ |
| 63 | uint32_t schema_parse_options; |
| 64 | uint32_t schema_print_options; |
| 65 | |
| 66 | /* specification of printing schema node subtree, option --schema-node */ |
| 67 | const char *schema_node_path; |
| 68 | const struct lysc_node *schema_node; |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 69 | const char *submodule; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 70 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 71 | /* name of file containing explicit context passed to callback |
| 72 | * for schema-mount extension. This also causes a callback to |
| 73 | * be registered. |
| 74 | */ |
| 75 | char *schema_context_filename; |
| 76 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 77 | /* value of --format in case of schema format */ |
| 78 | LYS_OUTFORMAT schema_out_format; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 79 | ly_bool feature_param_format; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * data |
| 83 | */ |
| 84 | /* various options based on --type option */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 85 | enum lyd_type data_type; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 86 | uint32_t data_parse_options; |
| 87 | uint32_t data_validate_options; |
| 88 | uint32_t data_print_options; |
| 89 | |
| 90 | /* flag for --merge option */ |
| 91 | uint8_t data_merge; |
| 92 | |
| 93 | /* value of --format in case of data format */ |
| 94 | LYD_FORMAT data_out_format; |
| 95 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 96 | /* value of --in-format in case of data format */ |
| 97 | LYD_FORMAT data_in_format; |
| 98 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 99 | /* input data files (struct cmdline_file *) */ |
| 100 | struct ly_set data_inputs; |
| 101 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 102 | /* storage for --operational */ |
| 103 | struct cmdline_file data_operational; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 104 | |
| 105 | /* storage for --reply-rpc */ |
| 106 | struct cmdline_file reply_rpc; |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 107 | |
| 108 | /* storage for --data-xpath */ |
| 109 | struct ly_set data_xpath; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | static void |
| 113 | erase_context(struct context *c) |
| 114 | { |
| 115 | /* data */ |
| 116 | ly_set_erase(&c->data_inputs, free_cmdline_file); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 117 | ly_in_free(c->data_operational.in, 1); |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 118 | ly_set_erase(&c->data_xpath, NULL); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 119 | |
| 120 | /* schema */ |
| 121 | ly_set_erase(&c->schema_features, free_features); |
| 122 | ly_set_erase(&c->schema_modules, NULL); |
| 123 | |
| 124 | /* context */ |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 125 | free(c->searchpaths); |
| 126 | c->searchpaths = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 127 | |
aPiecek | adb01c4 | 2023-06-02 08:28:52 +0200 | [diff] [blame] | 128 | /* --reply-rpc */ |
| 129 | ly_in_free(c->reply_rpc.in, 1); |
| 130 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 131 | ly_out_free(c->out, NULL, 0); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 132 | ly_ctx_destroy(c->ctx); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 133 | |
| 134 | if (c->schema_context_filename) { |
| 135 | free(c->schema_context_filename); |
| 136 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void |
| 140 | version(void) |
| 141 | { |
| 142 | printf("yanglint %s\n", PROJECT_VERSION); |
| 143 | } |
| 144 | |
| 145 | static void |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 146 | help(int shortout) |
| 147 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 148 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 149 | printf("Example usage:\n" |
| 150 | " yanglint [-f { yang | yin | info}] <schema>...\n" |
| 151 | " Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n" |
| 152 | " them in the specified format.\n\n" |
| 153 | " yanglint [-f { xml | json }] <schema>... <file>...\n" |
| 154 | " Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n" |
| 155 | " printing them in the specified format.\n\n" |
| 156 | " yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n" |
| 157 | " Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n" |
aPiecek | a40764b | 2023-04-27 15:34:56 +0200 | [diff] [blame] | 158 | " <operational-file> with possible references to the operational datastore data.\n" |
| 159 | " To validate nested-notification or action, the <operational-file> is required.\n\n" |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 160 | " yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n" |
| 161 | " Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n" |
| 162 | " using <operational-file> with possible references to the operational datastore data.\n\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 163 | " yanglint\n" |
| 164 | " Starts interactive mode with more features.\n\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 165 | |
| 166 | if (shortout) { |
| 167 | return; |
| 168 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 169 | printf("Options:\n" |
| 170 | " -h, --help Show this help message and exit.\n" |
| 171 | " -v, --version Show version number and exit.\n" |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 172 | " -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n" |
| 173 | " a second time, show even debug messages.\n" |
| 174 | " -Q, --quiet Decrease libyang verbosity and hide warnings. If specified a second\n" |
| 175 | " time, hide errors so no libyang messages are printed.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 176 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 177 | printf(" -f FORMAT, --format=FORMAT\n" |
| 178 | " Convert input into FORMAT. Supported formats: \n" |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 179 | " yang, yin, tree, info and feature-param for schemas,\n" |
Michal Vasko | d8b0e77 | 2022-03-18 13:36:04 +0100 | [diff] [blame] | 180 | " xml, json, and lyb for data.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 181 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 182 | printf(" -I FORMAT, --in-format=FORMAT\n" |
| 183 | " Load the data in one of the following formats:\n" |
| 184 | " xml, json, lyb\n" |
| 185 | " If input format not specified, it is detected from the file extension.\n\n"); |
| 186 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 187 | printf(" -p PATH, --path=PATH\n" |
| 188 | " Search path for schema (YANG/YIN) modules. The option can be\n" |
| 189 | " used multiple times. The current working directory and the\n" |
aPiecek | 4f306da | 2023-03-27 09:06:07 +0200 | [diff] [blame] | 190 | " path of the module being added is used implicitly. Subdirectories\n" |
| 191 | " are also searched\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 192 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 193 | printf(" -D, --disable-searchdir\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 194 | " Do not implicitly search in current working directory for\n" |
| 195 | " schema modules. If specified a second time, do not even\n" |
| 196 | " search in the module directory (all modules must be \n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 197 | " explicitly specified).\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 198 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 199 | printf(" -F FEATURES, --features=FEATURES\n" |
Michal Vasko | 5974087 | 2021-11-22 10:01:34 +0100 | [diff] [blame] | 200 | " Specific module features to support in the form <module-name>:(<feature>,)*\n" |
| 201 | " Use <feature> '*' to enable all features of a module. This option can be\n" |
| 202 | " specified multiple times, to enable features in multiple modules. If this\n" |
| 203 | " option is not specified, all the features in all the implemented modules\n" |
| 204 | " are enabled.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 205 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 206 | printf(" -i, --make-implemented\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 207 | " Make the imported modules \"referenced\" from any loaded\n" |
| 208 | " module also implemented. If specified a second time, all the\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 209 | " modules are set implemented.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 210 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 211 | printf(" -P PATH, --schema-node=PATH\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 212 | " Print only the specified subtree of the schema.\n" |
| 213 | " The PATH is the XPath subset mentioned in documentation as\n" |
| 214 | " the Path format. The option can be combined with --single-node\n" |
| 215 | " option to print information only about the specified node.\n" |
| 216 | " -q, --single-node\n" |
| 217 | " Supplement to the --schema-node option to print information\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 218 | " only about a single node specified as PATH argument.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 219 | |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 220 | printf(" -s SUBMODULE, --submodule=SUBMODULE\n" |
| 221 | " Print the specific submodule instead of the main module.\n\n"); |
| 222 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 223 | printf(" -x FILE, --ext-data=FILE\n" |
| 224 | " File containing the specific data required by an extension. Required by\n" |
Michal Vasko | 1466280 | 2022-11-08 08:45:46 +0100 | [diff] [blame] | 225 | " the schema-mount extension, for example, when the operational data are\n" |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 226 | " expected in the file. File format is guessed.\n\n"); |
| 227 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 228 | printf(" -n, --not-strict\n" |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 229 | " Do not require strict data parsing (silently skip unknown data),\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 230 | " has no effect for schemas.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 231 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 232 | printf(" -e, --present Validate only with the schema modules whose data actually\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 233 | " exist in the provided input data files. Takes effect only\n" |
| 234 | " with the 'data' or 'config' TYPEs. Used to avoid requiring\n" |
| 235 | " mandatory nodes from modules which data are not present in the\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 236 | " provided input data files.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 237 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 238 | printf(" -t TYPE, --type=TYPE\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 239 | " Specify data tree type in the input data file(s):\n" |
| 240 | " data - Complete datastore with status data (default type).\n" |
| 241 | " config - Configuration datastore (without status data).\n" |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 242 | " get - Data returned by the NETCONF <get> operation.\n" |
| 243 | " getconfig - Data returned by the NETCONF <get-config> operation.\n" |
| 244 | " edit - Config content of the NETCONF <edit-config> operation.\n" |
| 245 | " rpc - Invocation of a YANG RPC/action, defined as input.\n" |
| 246 | " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n" |
| 247 | " envelopes <rpc> or <action>.\n" |
| 248 | " reply - Reply to a YANG RPC/action, defined as output. Note that\n" |
| 249 | " the reply data are expected inside a container representing\n" |
| 250 | " the original RPC/action invocation.\n" |
| 251 | " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n" |
| 252 | " envelope <rpc-reply> with output data nodes as direct\n" |
| 253 | " descendants. The original RPC/action invocation is expected\n" |
| 254 | " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n" |
| 255 | " notif - Notification instance of a YANG notification.\n" |
| 256 | " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n" |
| 257 | " envelope <notification> with element <eventTime> and its\n" |
| 258 | " sibling as the actual notification.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 259 | |
| 260 | printf(" -d MODE, --default=MODE\n" |
| 261 | " Print data with default values, according to the MODE\n" |
| 262 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 263 | " must be loaded):\n" |
| 264 | " all - Add missing default nodes.\n" |
| 265 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 266 | " nodes with the attribute.\n" |
| 267 | " trim - Remove all nodes with a default value.\n" |
| 268 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"); |
| 269 | |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 270 | printf(" -E XPATH, --data-xpath=XPATH\n" |
aPiecek | 4195527 | 2023-05-10 16:10:17 +0200 | [diff] [blame] | 271 | " Evaluate XPATH expression over the data and print the nodes satisfying\n" |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 272 | " the expression. The output format is specific and the option cannot\n" |
| 273 | " be combined with the -f and -d options. Also all the data\n" |
| 274 | " inputs are merged into a single data tree where the expression\n" |
| 275 | " is evaluated, so the -m option is always set implicitly.\n\n"); |
| 276 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 277 | printf(" -l, --list Print info about the loaded schemas.\n" |
| 278 | " (i - imported module, I - implemented module)\n" |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 279 | " In case the '-f' option with data encoding is specified,\n" |
| 280 | " the list is printed as \"ietf-yang-library\" data.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 281 | |
| 282 | printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n" |
| 283 | " The limit of the maximum line length on which the 'tree'\n" |
| 284 | " format will try to be printed.\n\n"); |
| 285 | |
| 286 | printf(" -o OUTFILE, --output=OUTFILE\n" |
| 287 | " Write the output to OUTFILE instead of stdout.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 288 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 289 | printf(" -O FILE, --operational=FILE\n" |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 290 | " Provide optional data to extend validation of the '(nc-)rpc',\n" |
| 291 | " '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n" |
| 292 | " the operational datastore referenced from the operation.\n" |
aPiecek | a40764b | 2023-04-27 15:34:56 +0200 | [diff] [blame] | 293 | " In case of a nested notification or action, its parent existence\n" |
| 294 | " is also checked in these operational data.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 295 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 296 | printf(" -R FILE, --reply-rpc=FILE\n" |
| 297 | " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n" |
| 298 | " is supposed to contain the source 'nc-rpc' operation of the reply.\n\n"); |
| 299 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 300 | printf(" -m, --merge Merge input data files into a single tree and validate at\n" |
Michal Vasko | 06158fb | 2022-03-29 12:13:16 +0200 | [diff] [blame] | 301 | " once. The option has effect only for 'data' and 'config' TYPEs.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 302 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 303 | printf(" -y, --yang-library\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 304 | " Load and implement internal \"ietf-yang-library\" YANG module.\n" |
| 305 | " Note that this module includes definitions of mandatory state\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 306 | " data that can result in unexpected data validation errors.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 307 | |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 308 | printf(" -Y FILE, --yang-library-file=FILE\n" |
| 309 | " Parse FILE with \"ietf-yang-library\" data and use them to\n" |
| 310 | " create an exact YANG schema context. If specified, the '-F'\n" |
| 311 | " parameter (enabled features) is ignored.\n\n"); |
| 312 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 313 | printf(" -X, --extended-leafref\n" |
| 314 | " Allow usage of deref() XPath function within leafref\n\n"); |
| 315 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 316 | #ifndef NDEBUG |
| 317 | printf(" -G GROUPS, --debug=GROUPS\n" |
| 318 | " Enable printing of specific debugging message group\n" |
| 319 | " (nothing will be printed unless verbosity is set to debug):\n" |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 320 | " <group>[,<group>]* (dict, xpath, dep-sets)\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 321 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 324 | static void |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 325 | libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 326 | { |
| 327 | char *levstr; |
| 328 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 329 | switch (level) { |
| 330 | case LY_LLERR: |
| 331 | levstr = "err :"; |
| 332 | break; |
| 333 | case LY_LLWRN: |
| 334 | levstr = "warn:"; |
| 335 | break; |
| 336 | case LY_LLVRB: |
| 337 | levstr = "verb:"; |
| 338 | break; |
| 339 | default: |
| 340 | levstr = "dbg :"; |
| 341 | break; |
| 342 | } |
| 343 | if (path) { |
| 344 | fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path); |
| 345 | } else { |
| 346 | fprintf(stderr, "libyang %s %s\n", levstr, msg); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 350 | static struct schema_features * |
| 351 | get_features_not_applied(const struct ly_set *fset) |
| 352 | { |
| 353 | for (uint32_t u = 0; u < fset->count; ++u) { |
| 354 | struct schema_features *sf = fset->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 355 | |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 356 | if (!sf->applied) { |
| 357 | return sf; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return NULL; |
| 362 | } |
| 363 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 364 | static int |
| 365 | fill_context_inputs(int argc, char *argv[], struct context *c) |
| 366 | { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 367 | struct ly_in *in = NULL; |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 368 | struct schema_features *sf; |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 369 | struct lys_module *mod; |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 370 | const char *all_features[] = {"*", NULL}; |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 371 | char *dir = NULL, *module = NULL; |
| 372 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 373 | /* Create libyang context. */ |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 374 | if (c->yang_lib_file) { |
| 375 | /* ignore features */ |
| 376 | ly_set_erase(&c->schema_features, free_features); |
| 377 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 378 | if (ly_ctx_new_ylpath(c->searchpaths, c->yang_lib_file, LYD_UNKNOWN, c->ctx_options, &c->ctx)) { |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 379 | YLMSG_E("Unable to modify libyang context with yang-library data.\n"); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 380 | return -1; |
| 381 | } |
| 382 | } else { |
| 383 | /* set imp feature flag if all should be enabled */ |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 384 | c->ctx_options |= !c->schema_features.count ? LY_CTX_ENABLE_IMP_FEATURES : 0; |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 385 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 386 | if (ly_ctx_new(c->searchpaths, c->ctx_options, &c->ctx)) { |
| 387 | YLMSG_E("Unable to create libyang context\n"); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 388 | return -1; |
| 389 | } |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 390 | } |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 391 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 392 | /* set callback providing run-time extension instance data */ |
| 393 | if (c->schema_context_filename) { |
| 394 | ly_ctx_set_ext_data_clb(c->ctx, ext_data_clb, c->schema_context_filename); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 395 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 396 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 397 | /* process the operational and/or reply RPC content if any */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 398 | if (c->data_operational.path) { |
| 399 | if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) { |
| 400 | return -1; |
| 401 | } |
| 402 | } |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 403 | if (c->reply_rpc.path) { |
| 404 | if (get_input(c->reply_rpc.path, NULL, &c->reply_rpc.format, &c->reply_rpc.in)) { |
| 405 | return -1; |
| 406 | } |
| 407 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 408 | |
| 409 | for (int i = 0; i < argc - optind; i++) { |
| 410 | LYS_INFORMAT format_schema = LYS_IN_UNKNOWN; |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 411 | LYD_FORMAT format_data = c->data_in_format; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 412 | |
| 413 | if (get_input(argv[optind + i], &format_schema, &format_data, &in)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 414 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (format_schema) { |
| 418 | LY_ERR ret; |
| 419 | uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */ |
Michal Vasko | 5974087 | 2021-11-22 10:01:34 +0100 | [diff] [blame] | 420 | const char **features; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 421 | |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 422 | /* parse the input */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 423 | if (parse_schema_path(argv[optind + i], &dir, &module)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 424 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 425 | } |
| 426 | |
aPiecek | a6d45b5 | 2023-05-19 11:48:59 +0200 | [diff] [blame] | 427 | mod = NULL; |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 428 | if (c->yang_lib_file) { |
| 429 | /* just get the module, it should already be parsed */ |
| 430 | mod = ly_ctx_get_module_implemented(c->ctx, module); |
aPiecek | a6d45b5 | 2023-05-19 11:48:59 +0200 | [diff] [blame] | 431 | } |
| 432 | if (!mod) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 433 | /* add temporarily also the path of the module itself */ |
| 434 | if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) { |
| 435 | path_unset = 0; |
| 436 | } |
| 437 | |
| 438 | /* get features list for this module */ |
| 439 | if (!c->schema_features.count) { |
| 440 | features = all_features; |
| 441 | } else { |
| 442 | get_features(&c->schema_features, module, &features); |
| 443 | } |
| 444 | |
| 445 | /* parse module */ |
| 446 | ret = lys_parse(c->ctx, in, format_schema, features, &mod); |
| 447 | ly_ctx_unset_searchdir_last(c->ctx, path_unset); |
| 448 | if (ret) { |
| 449 | YLMSG_E("Parsing schema module \"%s\" failed.\n", argv[optind + i]); |
| 450 | goto error; |
| 451 | } |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 454 | /* temporary cleanup */ |
| 455 | free(dir); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 456 | dir = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 457 | free(module); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 458 | module = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 459 | |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 460 | if (c->schema_out_format || c->feature_param_format) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 461 | /* module will be printed */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 462 | if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) { |
| 463 | 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] | 464 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 465 | } |
| 466 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 467 | } else if (format_data) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 468 | 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] | 469 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 470 | } |
Radek Krejci | f53b0d5 | 2020-12-03 11:29:24 +0100 | [diff] [blame] | 471 | in = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 472 | } |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 473 | |
| 474 | ly_in_free(in, 1); |
| 475 | in = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 478 | /* check that all specified features were applied, apply now if possible */ |
| 479 | while ((sf = get_features_not_applied(&c->schema_features))) { |
| 480 | /* try to find implemented or the latest revision of this module */ |
| 481 | mod = ly_ctx_get_module_implemented(c->ctx, sf->mod_name); |
| 482 | if (!mod) { |
| 483 | mod = ly_ctx_get_module_latest(c->ctx, sf->mod_name); |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 484 | } |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 485 | if (!mod) { |
| 486 | YLMSG_E("Specified features not applied, module \"%s\" not loaded.\n", sf->mod_name); |
| 487 | goto error; |
| 488 | } |
| 489 | |
| 490 | /* we have the module, implement it if needed and enable the specific features */ |
| 491 | if (lys_set_implemented(mod, (const char **)sf->features)) { |
| 492 | YLMSG_E("Implementing module \"%s\" failed.\n", mod->name); |
| 493 | goto error; |
| 494 | } |
| 495 | sf->applied = 1; |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 498 | return 0; |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 499 | |
| 500 | error: |
| 501 | ly_in_free(in, 1); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 502 | free(dir); |
| 503 | free(module); |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 504 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /** |
| 508 | * @brief Process command line options and store the settings into the context. |
| 509 | * |
| 510 | * return -1 in case of error; |
| 511 | * return 0 in case of success and ready to process |
| 512 | * return 1 in case of success, but expect to exit. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 513 | */ |
| 514 | static int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 515 | fill_context(int argc, char *argv[], struct context *c) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 516 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 517 | int ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 518 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 519 | int opt, opt_index; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 520 | struct option options[] = { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 521 | {"help", no_argument, NULL, 'h'}, |
| 522 | {"version", no_argument, NULL, 'v'}, |
| 523 | {"verbose", no_argument, NULL, 'V'}, |
| 524 | {"quiet", no_argument, NULL, 'Q'}, |
| 525 | {"format", required_argument, NULL, 'f'}, |
| 526 | {"path", required_argument, NULL, 'p'}, |
Michal Vasko | d8b0e77 | 2022-03-18 13:36:04 +0100 | [diff] [blame] | 527 | {"disable-searchdir", no_argument, NULL, 'D'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 528 | {"features", required_argument, NULL, 'F'}, |
| 529 | {"make-implemented", no_argument, NULL, 'i'}, |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 530 | {"in-format", required_argument, NULL, 'I'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 531 | {"schema-node", required_argument, NULL, 'P'}, |
| 532 | {"single-node", no_argument, NULL, 'q'}, |
| 533 | {"submodule", required_argument, NULL, 's'}, |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 534 | {"ext-data", required_argument, NULL, 'x'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 535 | {"not-strict", no_argument, NULL, 'n'}, |
| 536 | {"present", no_argument, NULL, 'e'}, |
| 537 | {"type", required_argument, NULL, 't'}, |
| 538 | {"default", required_argument, NULL, 'd'}, |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 539 | {"data-xpath", required_argument, NULL, 'E'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 540 | {"list", no_argument, NULL, 'l'}, |
| 541 | {"tree-line-length", required_argument, NULL, 'L'}, |
| 542 | {"output", required_argument, NULL, 'o'}, |
| 543 | {"operational", required_argument, NULL, 'O'}, |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 544 | {"reply-rpc", required_argument, NULL, 'R'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 545 | {"merge", no_argument, NULL, 'm'}, |
| 546 | {"yang-library", no_argument, NULL, 'y'}, |
| 547 | {"yang-library-file", required_argument, NULL, 'Y'}, |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 548 | {"extended-leafref", no_argument, NULL, 'X'}, |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 549 | #ifndef NDEBUG |
| 550 | {"debug", required_argument, NULL, 'G'}, |
| 551 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 552 | {NULL, 0, NULL, 0} |
| 553 | }; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 554 | uint8_t data_type_set = 0; |
aPiecek | 7941e32 | 2023-05-10 15:44:41 +0200 | [diff] [blame] | 555 | uint32_t temp_lo = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 556 | |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 557 | c->ctx_options = YL_DEFAULT_CTX_OPTIONS; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 558 | c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS; |
Michal Vasko | 05eaf83 | 2023-02-10 09:21:46 +0100 | [diff] [blame] | 559 | c->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS; |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 560 | c->line_length = 0; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 561 | |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 562 | opterr = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 563 | #ifndef NDEBUG |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 564 | while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:G:", options, &opt_index)) != -1) |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 565 | #else |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 566 | while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:", options, &opt_index)) != -1) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 567 | #endif |
Michal Vasko | 03fe620 | 2022-07-22 16:23:13 +0200 | [diff] [blame] | 568 | { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 569 | switch (opt) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 570 | case 'h': /* --help */ |
| 571 | help(0); |
| 572 | return 1; |
| 573 | |
| 574 | case 'v': /* --version */ |
| 575 | version(); |
| 576 | return 1; |
| 577 | |
| 578 | case 'V': { /* --verbose */ |
| 579 | LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 580 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 581 | if (verbosity < LY_LLDBG) { |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 582 | ++verbosity; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 583 | } |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 584 | ly_log_level(verbosity); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 585 | break; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 586 | } /* case 'V' */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 587 | |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 588 | case 'Q': { /* --quiet */ |
| 589 | LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 590 | |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 591 | if (verbosity == LY_LLERR) { |
| 592 | /* turn logging off */ |
| 593 | ly_log_options(LY_LOSTORE_LAST); |
| 594 | } else if (verbosity > LY_LLERR) { |
| 595 | --verbosity; |
| 596 | } |
| 597 | ly_log_level(verbosity); |
| 598 | break; |
| 599 | } /* case 'Q' */ |
| 600 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 601 | case 'f': /* --format */ |
| 602 | if (!strcasecmp(optarg, "yang")) { |
| 603 | c->schema_out_format = LYS_OUT_YANG; |
| 604 | c->data_out_format = 0; |
| 605 | } else if (!strcasecmp(optarg, "yin")) { |
| 606 | c->schema_out_format = LYS_OUT_YIN; |
| 607 | c->data_out_format = 0; |
| 608 | } else if (!strcasecmp(optarg, "info")) { |
| 609 | c->schema_out_format = LYS_OUT_YANG_COMPILED; |
| 610 | c->data_out_format = 0; |
| 611 | } else if (!strcasecmp(optarg, "tree")) { |
| 612 | c->schema_out_format = LYS_OUT_TREE; |
| 613 | c->data_out_format = 0; |
| 614 | } else if (!strcasecmp(optarg, "xml")) { |
| 615 | c->schema_out_format = 0; |
| 616 | c->data_out_format = LYD_XML; |
| 617 | } else if (!strcasecmp(optarg, "json")) { |
| 618 | c->schema_out_format = 0; |
| 619 | c->data_out_format = LYD_JSON; |
Michal Vasko | d8b0e77 | 2022-03-18 13:36:04 +0100 | [diff] [blame] | 620 | } else if (!strcasecmp(optarg, "lyb")) { |
| 621 | c->schema_out_format = 0; |
| 622 | c->data_out_format = LYD_LYB; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 623 | } else if (!strcasecmp(optarg, "feature-param")) { |
| 624 | c->feature_param_format = 1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 625 | } else { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 626 | YLMSG_E("Unknown output format %s\n", optarg); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 627 | help(1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 628 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 629 | } |
| 630 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 631 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 632 | case 'I': /* --in-format */ |
| 633 | if (!strcasecmp(optarg, "xml")) { |
| 634 | c->data_in_format = LYD_XML; |
| 635 | } else if (!strcasecmp(optarg, "json")) { |
| 636 | c->data_in_format = LYD_JSON; |
| 637 | } else if (!strcasecmp(optarg, "lyb")) { |
| 638 | c->data_in_format = LYD_LYB; |
| 639 | } else { |
| 640 | YLMSG_E("Unknown input format %s\n", optarg); |
| 641 | help(1); |
| 642 | return -1; |
| 643 | } |
| 644 | break; |
| 645 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 646 | case 'p': { /* --path */ |
| 647 | struct stat st; |
| 648 | |
| 649 | if (stat(optarg, &st) == -1) { |
| 650 | YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno)); |
| 651 | return -1; |
| 652 | } |
| 653 | if (!S_ISDIR(st.st_mode)) { |
| 654 | YLMSG_E("Provided search path is not a directory.\n"); |
| 655 | return -1; |
| 656 | } |
| 657 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 658 | if (searchpath_strcat(&c->searchpaths, optarg)) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 659 | YLMSG_E("Storing searchpath failed.\n"); |
| 660 | return -1; |
| 661 | } |
| 662 | |
| 663 | break; |
| 664 | } /* case 'p' */ |
| 665 | |
| 666 | case 'D': /* --disable-search */ |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 667 | if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 668 | YLMSG_W("The -D option specified too many times.\n"); |
| 669 | } |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 670 | if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) { |
| 671 | c->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 672 | c->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 673 | } else { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 674 | c->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 675 | } |
| 676 | break; |
| 677 | |
| 678 | case 'F': /* --features */ |
| 679 | if (parse_features(optarg, &c->schema_features)) { |
| 680 | return -1; |
| 681 | } |
| 682 | break; |
| 683 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 684 | case 'i': /* --make-implemented */ |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 685 | if (c->ctx_options & LY_CTX_REF_IMPLEMENTED) { |
| 686 | c->ctx_options &= ~LY_CTX_REF_IMPLEMENTED; |
| 687 | c->ctx_options |= LY_CTX_ALL_IMPLEMENTED; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 688 | } else { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 689 | c->ctx_options |= LY_CTX_REF_IMPLEMENTED; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 690 | } |
| 691 | break; |
| 692 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 693 | case 'P': /* --schema-node */ |
| 694 | c->schema_node_path = optarg; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 695 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 696 | |
| 697 | case 'q': /* --single-node */ |
| 698 | c->schema_print_options |= LYS_PRINT_NO_SUBSTMT; |
| 699 | break; |
| 700 | |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 701 | case 's': /* --submodule */ |
| 702 | c->submodule = optarg; |
| 703 | break; |
| 704 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 705 | case 'x': /* --ext-data */ |
| 706 | c->schema_context_filename = strdup(optarg); |
| 707 | break; |
| 708 | |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 709 | case 'n': /* --not-strict */ |
| 710 | c->data_parse_options &= ~LYD_PARSE_STRICT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 711 | break; |
| 712 | |
| 713 | case 'e': /* --present */ |
| 714 | c->data_validate_options |= LYD_VALIDATE_PRESENT; |
| 715 | break; |
| 716 | |
| 717 | case 't': /* --type */ |
| 718 | if (data_type_set) { |
| 719 | YLMSG_E("The data type (-t) cannot be set multiple times.\n"); |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | if (!strcasecmp(optarg, "config")) { |
| 724 | c->data_parse_options |= LYD_PARSE_NO_STATE; |
Michal Vasko | f6bda33 | 2021-02-01 08:59:03 +0100 | [diff] [blame] | 725 | c->data_validate_options |= LYD_VALIDATE_NO_STATE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 726 | } else if (!strcasecmp(optarg, "get")) { |
| 727 | c->data_parse_options |= LYD_PARSE_ONLY; |
aPiecek | be6c060 | 2023-04-18 11:29:09 +0200 | [diff] [blame] | 728 | } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 729 | c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 730 | } else if (!strcasecmp(optarg, "rpc")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 731 | c->data_type = LYD_TYPE_RPC_YANG; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 732 | } else if (!strcasecmp(optarg, "nc-rpc")) { |
| 733 | c->data_type = LYD_TYPE_RPC_NETCONF; |
| 734 | } else if (!strcasecmp(optarg, "reply")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 735 | c->data_type = LYD_TYPE_REPLY_YANG; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 736 | } else if (!strcasecmp(optarg, "nc-reply")) { |
| 737 | c->data_type = LYD_TYPE_REPLY_NETCONF; |
| 738 | } else if (!strcasecmp(optarg, "notif")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 739 | c->data_type = LYD_TYPE_NOTIF_YANG; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 740 | } else if (!strcasecmp(optarg, "nc-notif")) { |
| 741 | c->data_type = LYD_TYPE_NOTIF_NETCONF; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 742 | } else if (!strcasecmp(optarg, "data")) { |
| 743 | /* default option */ |
| 744 | } else { |
| 745 | YLMSG_E("Unknown data tree type %s\n", optarg); |
| 746 | help(1); |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | data_type_set = 1; |
| 751 | break; |
| 752 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 753 | case 'd': /* --default */ |
| 754 | if (!strcasecmp(optarg, "all")) { |
| 755 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL; |
| 756 | } else if (!strcasecmp(optarg, "all-tagged")) { |
| 757 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG; |
| 758 | } else if (!strcasecmp(optarg, "trim")) { |
| 759 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM; |
| 760 | } else if (!strcasecmp(optarg, "implicit-tagged")) { |
| 761 | c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG; |
| 762 | } else { |
| 763 | YLMSG_E("Unknown default mode %s\n", optarg); |
| 764 | help(1); |
| 765 | return -1; |
| 766 | } |
| 767 | break; |
| 768 | |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 769 | case 'E': /* --data-xpath */ |
| 770 | if (ly_set_add(&c->data_xpath, optarg, 0, NULL)) { |
| 771 | YLMSG_E("Storing XPath \"%s\" failed.\n", optarg); |
| 772 | return -1; |
| 773 | } |
| 774 | break; |
| 775 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 776 | case 'l': /* --list */ |
| 777 | c->list = 1; |
| 778 | break; |
| 779 | |
| 780 | case 'L': /* --tree-line-length */ |
| 781 | c->line_length = atoi(optarg); |
| 782 | break; |
| 783 | |
| 784 | case 'o': /* --output */ |
| 785 | if (c->out) { |
| 786 | YLMSG_E("Only a single output can be specified.\n"); |
| 787 | return -1; |
| 788 | } else { |
| 789 | if (ly_out_new_filepath(optarg, &c->out)) { |
| 790 | YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 791 | return -1; |
| 792 | } |
| 793 | } |
| 794 | break; |
| 795 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 796 | case 'O': /* --operational */ |
| 797 | if (c->data_operational.path) { |
| 798 | YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n"); |
| 799 | return -1; |
| 800 | } |
| 801 | c->data_operational.path = optarg; |
| 802 | break; |
| 803 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 804 | case 'R': /* --reply-rpc */ |
| 805 | if (c->reply_rpc.path) { |
| 806 | YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n"); |
| 807 | return -1; |
| 808 | } |
| 809 | c->reply_rpc.path = optarg; |
| 810 | break; |
| 811 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 812 | case 'm': /* --merge */ |
| 813 | c->data_merge = 1; |
| 814 | break; |
| 815 | |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 816 | case 'y': /* --yang-library */ |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 817 | c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; |
| 818 | break; |
| 819 | |
| 820 | case 'Y': /* --yang-library-file */ |
| 821 | c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; |
| 822 | c->yang_lib_file = optarg; |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 823 | break; |
| 824 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 825 | case 'X': /* --extended-leafref */ |
| 826 | c->ctx_options |= LY_CTX_LEAFREF_EXTENDED; |
| 827 | break; |
| 828 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 829 | #ifndef NDEBUG |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 830 | case 'G': { /* --debug */ |
| 831 | uint32_t dbg_groups = 0; |
| 832 | const char *ptr = optarg; |
| 833 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 834 | while (ptr[0]) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 835 | if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 836 | dbg_groups |= LY_LDGDICT; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 837 | ptr += sizeof "dict" - 1; |
| 838 | } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 839 | dbg_groups |= LY_LDGXPATH; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 840 | ptr += sizeof "xpath" - 1; |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 841 | } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) { |
| 842 | dbg_groups |= LY_LDGDEPSETS; |
| 843 | ptr += sizeof "dep-sets" - 1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (ptr[0]) { |
| 847 | if (ptr[0] != ',') { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 848 | YLMSG_E("Unknown debug group string \"%s\"\n", optarg); |
| 849 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 850 | } |
| 851 | ++ptr; |
| 852 | } |
| 853 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 854 | ly_log_dbg_groups(dbg_groups); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 855 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 856 | } /* case 'G' */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 857 | #endif |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 858 | default: |
| 859 | YLMSG_E("Invalid option or missing argument: -%c\n", optopt); |
| 860 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 861 | } /* switch */ |
| 862 | } |
| 863 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 864 | /* additional checks for the options combinations */ |
| 865 | if (!c->list && (optind >= argc)) { |
| 866 | help(1); |
| 867 | YLMSG_E("Missing <schema> to process.\n"); |
| 868 | return 1; |
| 869 | } |
| 870 | |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 871 | if (c->data_xpath.count) { |
| 872 | c->data_merge = 1; |
| 873 | } |
| 874 | if (c->data_xpath.count && (c->schema_out_format || c->data_out_format)) { |
| 875 | YLMSG_E("The --format option cannot be combined with --data-xpath option.\n"); |
| 876 | return -1; |
| 877 | } |
| 878 | if (c->data_xpath.count && (c->data_print_options & LYD_PRINT_WD_MASK)) { |
| 879 | YLMSG_E("The --default option cannot be combined with --data-xpath option.\n"); |
| 880 | return -1; |
| 881 | } |
| 882 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 883 | if (c->data_merge) { |
| 884 | if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) { |
| 885 | /* switch off the option, incompatible input data type */ |
aPiecek | 3d46fa9 | 2023-05-10 08:30:39 +0200 | [diff] [blame] | 886 | YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 887 | c->data_merge = 0; |
| 888 | } else { |
| 889 | /* postpone validation after the merge of all the input data */ |
| 890 | c->data_parse_options |= LYD_PARSE_ONLY; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 894 | if (c->data_operational.path && !c->data_type) { |
aPiecek | ce48158 | 2023-04-26 15:44:59 +0200 | [diff] [blame] | 895 | YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 896 | c->data_operational.path = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 897 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 898 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 899 | if (c->reply_rpc.path && (c->data_type != LYD_TYPE_REPLY_NETCONF)) { |
aPiecek | ce48158 | 2023-04-26 15:44:59 +0200 | [diff] [blame] | 900 | YLMSG_W("Source RPC is needed only for NETCONF Reply input data type.\n"); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 901 | c->data_operational.path = NULL; |
| 902 | } else if (!c->reply_rpc.path && (c->data_type == LYD_TYPE_REPLY_NETCONF)) { |
| 903 | YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\n"); |
| 904 | return -1; |
| 905 | } |
| 906 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 907 | if ((c->schema_out_format != LYS_OUT_TREE) && c->line_length) { |
aPiecek | ce48158 | 2023-04-26 15:44:59 +0200 | [diff] [blame] | 908 | YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n"); |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 909 | } |
| 910 | |
aPiecek | a472348 | 2023-05-11 14:31:02 +0200 | [diff] [blame] | 911 | if (!c->out && (c->data_out_format == LYD_LYB)) { |
| 912 | YLMSG_E("The LYB format requires the -o option specified.\n"); |
| 913 | return -1; |
| 914 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 915 | /* default output stream */ |
| 916 | if (!c->out) { |
| 917 | if (ly_out_new_file(stdout, &c->out)) { |
| 918 | YLMSG_E("Unable to set stdout as output.\n"); |
| 919 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 920 | } |
| 921 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 922 | |
aPiecek | 7ac66d5 | 2021-05-20 07:54:07 +0200 | [diff] [blame] | 923 | if (c->schema_out_format == LYS_OUT_TREE) { |
| 924 | /* print tree from lysc_nodes */ |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 925 | c->ctx_options |= LY_CTX_SET_PRIV_PARSED; |
aPiecek | 7ac66d5 | 2021-05-20 07:54:07 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 928 | /* process input files provided as standalone command line arguments, |
| 929 | * schema modules are parsed and inserted into the context, |
| 930 | * data files are just checked and prepared into internal structures for further processing */ |
| 931 | ret = fill_context_inputs(argc, argv, c); |
| 932 | if (ret) { |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | /* the second batch of checks */ |
| 937 | if (c->schema_print_options && !c->schema_out_format) { |
| 938 | YLMSG_W("Schema printer options specified, but the schema output format is missing.\n"); |
| 939 | } |
| 940 | if (c->schema_parse_options && !c->schema_modules.count) { |
| 941 | YLMSG_W("Schema parser options specified, but no schema input file provided.\n"); |
| 942 | } |
| 943 | if (c->data_print_options && !c->data_out_format) { |
| 944 | YLMSG_W("data printer options specified, but the data output format is missing.\n"); |
| 945 | } |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 946 | 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] | 947 | YLMSG_W("Data parser options specified, but no data input file provided.\n"); |
| 948 | } |
| 949 | |
| 950 | if (c->schema_node_path) { |
aPiecek | 7941e32 | 2023-05-10 15:44:41 +0200 | [diff] [blame] | 951 | /* turn off logging so that the message is not repeated */ |
| 952 | ly_temp_log_options(&temp_lo); |
| 953 | /* search operation input */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 954 | c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0); |
| 955 | if (!c->schema_node) { |
aPiecek | 7941e32 | 2023-05-10 15:44:41 +0200 | [diff] [blame] | 956 | /* restore logging so an error may be displayed */ |
| 957 | ly_temp_log_options(NULL); |
| 958 | /* search operation output */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 959 | c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 960 | if (!c->schema_node) { |
| 961 | YLMSG_E("Invalid schema path.\n"); |
| 962 | return -1; |
| 963 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 964 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 965 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 966 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | int |
| 971 | main_ni(int argc, char *argv[]) |
| 972 | { |
| 973 | int ret = EXIT_SUCCESS, r; |
| 974 | struct context c = {0}; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 975 | char *features_output = NULL; |
| 976 | struct ly_set set = {0}; |
| 977 | uint32_t u; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 978 | |
| 979 | /* set callback for printing libyang messages */ |
| 980 | ly_set_log_clb(libyang_verbclb, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 981 | |
| 982 | r = fill_context(argc, argv, &c); |
| 983 | if (r < 0) { |
| 984 | ret = EXIT_FAILURE; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 985 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 986 | if (r) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 987 | goto cleanup; |
| 988 | } |
| 989 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 990 | /* do the required job - parse, validate, print */ |
| 991 | |
| 992 | if (c.list) { |
| 993 | /* print the list of schemas */ |
aPiecek | 3426262 | 2023-03-27 08:28:25 +0200 | [diff] [blame] | 994 | ret = print_list(c.out, c.ctx, c.data_out_format); |
| 995 | goto cleanup; |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 996 | } else { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 997 | if (c.feature_param_format) { |
| 998 | for (u = 0; u < c.schema_modules.count; u++) { |
| 999 | if (collect_features(c.schema_modules.objs[u], &set)) { |
| 1000 | YLMSG_E("Unable to read features from a module.\n"); |
| 1001 | goto cleanup; |
| 1002 | } |
| 1003 | if (generate_features_output(c.schema_modules.objs[u], &set, &features_output)) { |
| 1004 | YLMSG_E("Unable to generate feature command output.\n"); |
| 1005 | goto cleanup; |
| 1006 | } |
| 1007 | ly_set_erase(&set, NULL); |
| 1008 | } |
| 1009 | ly_print(c.out, "%s\n", features_output); |
| 1010 | } else if (c.schema_out_format) { |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 1011 | if (c.schema_node) { |
aPiecek | 146fd19 | 2023-03-30 11:22:16 +0200 | [diff] [blame] | 1012 | ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, c.line_length, c.schema_print_options); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1013 | if (ret) { |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 1014 | 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] | 1015 | goto cleanup; |
| 1016 | } |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 1017 | } else if (c.submodule) { |
| 1018 | const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(c.ctx, c.submodule); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1019 | |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 1020 | if (!submod) { |
| 1021 | YLMSG_E("Unable to find submodule %s.\n", c.submodule); |
| 1022 | goto cleanup; |
| 1023 | } |
| 1024 | |
| 1025 | ret = lys_print_submodule(c.out, submod, c.schema_out_format, c.line_length, c.schema_print_options); |
| 1026 | if (ret) { |
| 1027 | YLMSG_E("Unable to print submodule %s.\n", submod->name); |
| 1028 | goto cleanup; |
| 1029 | } |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 1030 | } else { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 1031 | for (u = 0; u < c.schema_modules.count; ++u) { |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 1032 | ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format, |
| 1033 | c.line_length, c.schema_print_options); |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 1034 | /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */ |
| 1035 | if ((c.schema_out_format == LYS_OUT_TREE) && (u + 1 < c.schema_modules.count)) { |
| 1036 | ly_print(c.out, "\n"); |
| 1037 | } |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 1038 | if (ret) { |
| 1039 | YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name); |
| 1040 | goto cleanup; |
| 1041 | } |
| 1042 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1043 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1044 | } |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 1045 | |
| 1046 | /* do the data validation despite the schema was printed */ |
| 1047 | if (c.data_inputs.size) { |
Michal Vasko | 0f1e456 | 2022-03-29 11:57:57 +0200 | [diff] [blame] | 1048 | ret = process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out, c.data_parse_options, |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 1049 | c.data_validate_options, c.data_print_options, &c.data_operational, &c.reply_rpc, &c.data_inputs, |
| 1050 | &c.data_xpath); |
Michal Vasko | 0f1e456 | 2022-03-29 11:57:57 +0200 | [diff] [blame] | 1051 | if (ret) { |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 1052 | goto cleanup; |
| 1053 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1054 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1055 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1056 | |
| 1057 | cleanup: |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1058 | /* cleanup */ |
| 1059 | erase_context(&c); |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 1060 | free(features_output); |
| 1061 | ly_set_erase(&set, NULL); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1062 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1063 | return ret; |
| 1064 | } |