blob: c2d5c56a3e4a91c908cd4387a562a907e6b588f8 [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file main_ni.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief libyang's yanglint tool - non-interactive code
Radek Krejcied5acc52019-04-25 15:57:04 +02006 *
Michal Vasko193dacd2022-10-13 08:43:05 +02007 * Copyright (c) 2020 - 2022 CESNET, z.s.p.o.
Radek Krejcied5acc52019-04-25 15:57:04 +02008 *
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 Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
Radek Krejcied5acc52019-04-25 15:57:04 +020017
Radek Krejcie9f13b12020-11-09 17:42:04 +010018#include <errno.h>
19#include <getopt.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020021#include <stdio.h>
22#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020023#include <string.h>
Radek Krejci47fab892020-11-05 17:02:41 +010024#include <strings.h>
Radek Krejcie9f13b12020-11-09 17:42:04 +010025#include <sys/stat.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020026
Radek Krejcied5acc52019-04-25 15:57:04 +020027#include "libyang.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020028#include "plugins_exts.h"
Radek Krejcied5acc52019-04-25 15:57:04 +020029
Radek Krejcie9f13b12020-11-09 17:42:04 +010030#include "common.h"
aPiecek8f0b7882021-05-21 10:18:36 +020031#include "out.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "tools/config.h"
33
Radek Krejcie9f13b12020-11-09 17:42:04 +010034/**
35 * @brief Context structure to hold and pass variables in a structured form.
36 */
37struct context {
38 /* libyang context for the run */
Michal Vasko24aaf8e2022-02-04 11:15:26 +010039 const char *yang_lib_file;
40 uint16_t ctx_options;
Radek Krejcie9f13b12020-11-09 17:42:04 +010041 struct ly_ctx *ctx;
Radek Krejci535ea9f2020-05-29 16:01:05 +020042
Radek Krejcie9f13b12020-11-09 17:42:04 +010043 /* prepared output (--output option or stdout by default) */
44 struct ly_out *out;
Radek Krejci535ea9f2020-05-29 16:01:05 +020045
aPiecek912f3d52022-10-12 10:02:33 +020046 char *searchpaths;
Radek Krejcied5acc52019-04-25 15:57:04 +020047
Radek Krejcie9f13b12020-11-09 17:42:04 +010048 /* options flags */
49 uint8_t list; /* -l option to print list of schemas */
Radek Krejcied5acc52019-04-25 15:57:04 +020050
aPiecek2cfeeae2021-04-21 13:17:34 +020051 /* line length for 'tree' format */
52 size_t line_length; /* --tree-line-length */
53
Radek Krejcie9f13b12020-11-09 17:42:04 +010054 /*
55 * schema
56 */
Michal Vaskoa9a98612021-11-22 10:00:27 +010057 /* set schema modules' features via --features option (struct schema_features *) */
Radek Krejcie9f13b12020-11-09 17:42:04 +010058 struct ly_set schema_features;
59
60 /* set of loaded schema modules (struct lys_module *) */
61 struct ly_set schema_modules;
62
63 /* options to parse and print schema modules */
64 uint32_t schema_parse_options;
65 uint32_t schema_print_options;
66
67 /* specification of printing schema node subtree, option --schema-node */
68 const char *schema_node_path;
69 const struct lysc_node *schema_node;
Michal Vaskofe74d1e2021-09-30 13:17:36 +020070 const char *submodule;
Radek Krejcie9f13b12020-11-09 17:42:04 +010071
ekinzie0ab8b302022-10-10 03:03:57 -040072 /* name of file containing explicit context passed to callback
73 * for schema-mount extension. This also causes a callback to
74 * be registered.
75 */
76 char *schema_context_filename;
77
Radek Krejcie9f13b12020-11-09 17:42:04 +010078 /* value of --format in case of schema format */
79 LYS_OUTFORMAT schema_out_format;
roman300b8782022-08-11 12:49:21 +020080 ly_bool feature_param_format;
Radek Krejcie9f13b12020-11-09 17:42:04 +010081
82 /*
83 * data
84 */
85 /* various options based on --type option */
Michal Vaskoe0665742021-02-11 11:08:44 +010086 enum lyd_type data_type;
Radek Krejcie9f13b12020-11-09 17:42:04 +010087 uint32_t data_parse_options;
88 uint32_t data_validate_options;
89 uint32_t data_print_options;
90
91 /* flag for --merge option */
92 uint8_t data_merge;
93
94 /* value of --format in case of data format */
95 LYD_FORMAT data_out_format;
96
97 /* input data files (struct cmdline_file *) */
98 struct ly_set data_inputs;
99
Radek Krejcie9f13b12020-11-09 17:42:04 +0100100 /* storage for --operational */
101 struct cmdline_file data_operational;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200102
103 /* storage for --reply-rpc */
104 struct cmdline_file reply_rpc;
aPiecek1b629762023-04-12 13:52:30 +0200105
106 /* storage for --data-xpath */
107 struct ly_set data_xpath;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100108};
109
110static void
111erase_context(struct context *c)
112{
113 /* data */
114 ly_set_erase(&c->data_inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100115 ly_in_free(c->data_operational.in, 1);
aPiecek1b629762023-04-12 13:52:30 +0200116 ly_set_erase(&c->data_xpath, NULL);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100117
118 /* schema */
119 ly_set_erase(&c->schema_features, free_features);
120 ly_set_erase(&c->schema_modules, NULL);
121
122 /* context */
aPiecek912f3d52022-10-12 10:02:33 +0200123 free(c->searchpaths);
124 c->searchpaths = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100125
aPiecekadb01c42023-06-02 08:28:52 +0200126 /* --reply-rpc */
127 ly_in_free(c->reply_rpc.in, 1);
128
Radek Krejcie9f13b12020-11-09 17:42:04 +0100129 ly_out_free(c->out, NULL, 0);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200130 ly_ctx_destroy(c->ctx);
ekinzie0ab8b302022-10-10 03:03:57 -0400131
132 if (c->schema_context_filename) {
133 free(c->schema_context_filename);
134 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100135}
136
137static void
138version(void)
139{
140 printf("yanglint %s\n", PROJECT_VERSION);
141}
142
143static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200144help(int shortout)
145{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100146
Michal Vasko3f08fb92022-04-21 09:52:35 +0200147 printf("Example usage:\n"
148 " yanglint [-f { yang | yin | info}] <schema>...\n"
149 " Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n"
150 " them in the specified format.\n\n"
151 " yanglint [-f { xml | json }] <schema>... <file>...\n"
152 " Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n"
153 " printing them in the specified format.\n\n"
154 " yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n"
155 " Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n"
aPieceka40764b2023-04-27 15:34:56 +0200156 " <operational-file> with possible references to the operational datastore data.\n"
157 " To validate nested-notification or action, the <operational-file> is required.\n\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +0200158 " yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n"
159 " Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n"
160 " using <operational-file> with possible references to the operational datastore data.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100161 " yanglint\n"
162 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200163
164 if (shortout) {
165 return;
166 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100167 printf("Options:\n"
168 " -h, --help Show this help message and exit.\n"
169 " -v, --version Show version number and exit.\n"
Michal Vaskob0d307b2021-11-25 11:15:16 +0100170 " -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n"
171 " a second time, show even debug messages.\n"
172 " -Q, --quiet Decrease libyang verbosity and hide warnings. If specified a second\n"
173 " time, hide errors so no libyang messages are printed.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100174
Michal Vaskoc40548b2021-09-30 13:05:47 +0200175 printf(" -f FORMAT, --format=FORMAT\n"
176 " Convert input into FORMAT. Supported formats: \n"
roman300b8782022-08-11 12:49:21 +0200177 " yang, yin, tree, info and feature-param for schemas,\n"
Michal Vaskod8b0e772022-03-18 13:36:04 +0100178 " xml, json, and lyb for data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200179
180 printf(" -p PATH, --path=PATH\n"
181 " Search path for schema (YANG/YIN) modules. The option can be\n"
182 " used multiple times. The current working directory and the\n"
aPiecek4f306da2023-03-27 09:06:07 +0200183 " path of the module being added is used implicitly. Subdirectories\n"
184 " are also searched\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100185
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200186 printf(" -D, --disable-searchdir\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100187 " Do not implicitly search in current working directory for\n"
188 " schema modules. If specified a second time, do not even\n"
189 " search in the module directory (all modules must be \n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200190 " explicitly specified).\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100191
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200192 printf(" -F FEATURES, --features=FEATURES\n"
Michal Vasko59740872021-11-22 10:01:34 +0100193 " Specific module features to support in the form <module-name>:(<feature>,)*\n"
194 " Use <feature> '*' to enable all features of a module. This option can be\n"
195 " specified multiple times, to enable features in multiple modules. If this\n"
196 " option is not specified, all the features in all the implemented modules\n"
197 " are enabled.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100198
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200199 printf(" -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100200 " Make the imported modules \"referenced\" from any loaded\n"
201 " module also implemented. If specified a second time, all the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200202 " modules are set implemented.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100203
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200204 printf(" -P PATH, --schema-node=PATH\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100205 " Print only the specified subtree of the schema.\n"
206 " The PATH is the XPath subset mentioned in documentation as\n"
207 " the Path format. The option can be combined with --single-node\n"
208 " option to print information only about the specified node.\n"
209 " -q, --single-node\n"
210 " Supplement to the --schema-node option to print information\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200211 " only about a single node specified as PATH argument.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100212
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200213 printf(" -s SUBMODULE, --submodule=SUBMODULE\n"
214 " Print the specific submodule instead of the main module.\n\n");
215
ekinzie0ab8b302022-10-10 03:03:57 -0400216 printf(" -x FILE, --ext-data=FILE\n"
217 " File containing the specific data required by an extension. Required by\n"
Michal Vasko14662802022-11-08 08:45:46 +0100218 " the schema-mount extension, for example, when the operational data are\n"
ekinzie0ab8b302022-10-10 03:03:57 -0400219 " expected in the file. File format is guessed.\n\n");
220
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200221 printf(" -n, --not-strict\n"
Michal Vasko667ce6b2021-01-25 15:00:27 +0100222 " Do not require strict data parsing (silently skip unknown data),\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200223 " has no effect for schemas.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100224
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200225 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100226 " exist in the provided input data files. Takes effect only\n"
227 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
228 " mandatory nodes from modules which data are not present in the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200229 " provided input data files.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100230
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200231 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100232 " Specify data tree type in the input data file(s):\n"
233 " data - Complete datastore with status data (default type).\n"
234 " config - Configuration datastore (without status data).\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +0200235 " get - Data returned by the NETCONF <get> operation.\n"
236 " getconfig - Data returned by the NETCONF <get-config> operation.\n"
237 " edit - Config content of the NETCONF <edit-config> operation.\n"
238 " rpc - Invocation of a YANG RPC/action, defined as input.\n"
239 " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n"
240 " envelopes <rpc> or <action>.\n"
241 " reply - Reply to a YANG RPC/action, defined as output. Note that\n"
242 " the reply data are expected inside a container representing\n"
243 " the original RPC/action invocation.\n"
244 " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n"
245 " envelope <rpc-reply> with output data nodes as direct\n"
246 " descendants. The original RPC/action invocation is expected\n"
247 " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n"
248 " notif - Notification instance of a YANG notification.\n"
249 " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n"
250 " envelope <notification> with element <eventTime> and its\n"
251 " sibling as the actual notification.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200252
253 printf(" -d MODE, --default=MODE\n"
254 " Print data with default values, according to the MODE\n"
255 " (to print attributes, ietf-netconf-with-defaults model\n"
256 " must be loaded):\n"
257 " all - Add missing default nodes.\n"
258 " all-tagged - Add missing default nodes and mark all the default\n"
259 " nodes with the attribute.\n"
260 " trim - Remove all nodes with a default value.\n"
261 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n");
262
aPiecek1b629762023-04-12 13:52:30 +0200263 printf(" -E XPATH, --data-xpath=XPATH\n"
aPiecek41955272023-05-10 16:10:17 +0200264 " Evaluate XPATH expression over the data and print the nodes satisfying\n"
aPiecek1b629762023-04-12 13:52:30 +0200265 " the expression. The output format is specific and the option cannot\n"
266 " be combined with the -f and -d options. Also all the data\n"
267 " inputs are merged into a single data tree where the expression\n"
268 " is evaluated, so the -m option is always set implicitly.\n\n");
269
Michal Vaskoc40548b2021-09-30 13:05:47 +0200270 printf(" -l, --list Print info about the loaded schemas.\n"
271 " (i - imported module, I - implemented module)\n"
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100272 " In case the '-f' option with data encoding is specified,\n"
273 " the list is printed as \"ietf-yang-library\" data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200274
275 printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
276 " The limit of the maximum line length on which the 'tree'\n"
277 " format will try to be printed.\n\n");
278
279 printf(" -o OUTFILE, --output=OUTFILE\n"
280 " Write the output to OUTFILE instead of stdout.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100281
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200282 printf(" -O FILE, --operational=FILE\n"
Michal Vasko9e81ce52022-04-29 10:16:21 +0200283 " Provide optional data to extend validation of the '(nc-)rpc',\n"
284 " '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n"
285 " the operational datastore referenced from the operation.\n"
aPieceka40764b2023-04-27 15:34:56 +0200286 " In case of a nested notification or action, its parent existence\n"
287 " is also checked in these operational data.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100288
Michal Vasko3f08fb92022-04-21 09:52:35 +0200289 printf(" -R FILE, --reply-rpc=FILE\n"
290 " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n"
291 " is supposed to contain the source 'nc-rpc' operation of the reply.\n\n");
292
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200293 printf(" -m, --merge Merge input data files into a single tree and validate at\n"
Michal Vasko06158fb2022-03-29 12:13:16 +0200294 " once. The option has effect only for 'data' and 'config' TYPEs.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100295
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200296 printf(" -y, --yang-library\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100297 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
298 " Note that this module includes definitions of mandatory state\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200299 " data that can result in unexpected data validation errors.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200300
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100301 printf(" -Y FILE, --yang-library-file=FILE\n"
302 " Parse FILE with \"ietf-yang-library\" data and use them to\n"
303 " create an exact YANG schema context. If specified, the '-F'\n"
304 " parameter (enabled features) is ignored.\n\n");
305
stewegd8e2fc92023-05-31 09:52:56 +0200306 printf(" -X, --extended-leafref\n"
307 " Allow usage of deref() XPath function within leafref\n\n");
308
Michal Vaskoc40548b2021-09-30 13:05:47 +0200309#ifndef NDEBUG
310 printf(" -G GROUPS, --debug=GROUPS\n"
311 " Enable printing of specific debugging message group\n"
312 " (nothing will be printed unless verbosity is set to debug):\n"
Michal Vasko27a4acb2021-11-22 10:03:46 +0100313 " <group>[,<group>]* (dict, xpath, dep-sets)\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200314#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200315}
316
Radek Krejcie9f13b12020-11-09 17:42:04 +0100317static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200318libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
319{
320 char *levstr;
321
Radek Krejcie9f13b12020-11-09 17:42:04 +0100322 switch (level) {
323 case LY_LLERR:
324 levstr = "err :";
325 break;
326 case LY_LLWRN:
327 levstr = "warn:";
328 break;
329 case LY_LLVRB:
330 levstr = "verb:";
331 break;
332 default:
333 levstr = "dbg :";
334 break;
335 }
336 if (path) {
337 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
338 } else {
339 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200340 }
341}
342
Michal Vasko686d8fc2021-11-22 10:03:23 +0100343static struct schema_features *
344get_features_not_applied(const struct ly_set *fset)
345{
346 for (uint32_t u = 0; u < fset->count; ++u) {
347 struct schema_features *sf = fset->objs[u];
Michal Vasko26bbb272022-08-02 14:54:33 +0200348
Michal Vasko686d8fc2021-11-22 10:03:23 +0100349 if (!sf->applied) {
350 return sf;
351 }
352 }
353
354 return NULL;
355}
356
ekinzie0ab8b302022-10-10 03:03:57 -0400357static LY_ERR
358ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
359{
360 struct ly_ctx *ctx;
361 struct lyd_node *data = NULL;
362
363 ctx = ext->module->ctx;
364 if (user_data) {
365 lyd_parse_data_path(ctx, user_data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &data);
366 }
367
368 *ext_data = data;
369 *ext_data_free = 1;
370 return LY_SUCCESS;
371}
372
aPiecek912f3d52022-10-12 10:02:33 +0200373static LY_ERR
374searchpath_strcat(char **searchpaths, const char *path)
375{
376 uint64_t len;
377 char *new;
378
379 if (!(*searchpaths)) {
380 *searchpaths = strdup(path);
381 return LY_SUCCESS;
382 }
383
384 len = strlen(*searchpaths) + strlen(path) + strlen(PATH_SEPARATOR);
385 new = realloc(*searchpaths, sizeof(char) * len + 1);
386 if (!new) {
387 return LY_EMEM;
388 }
389 strcat(new, PATH_SEPARATOR);
390 strcat(new, path);
391 *searchpaths = new;
392
393 return LY_SUCCESS;
394}
395
Radek Krejcie9f13b12020-11-09 17:42:04 +0100396static int
397fill_context_inputs(int argc, char *argv[], struct context *c)
398{
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100399 struct ly_in *in = NULL;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100400 struct schema_features *sf;
Michal Vaskoc91fb122021-11-23 09:00:17 +0100401 struct lys_module *mod;
aPiecek912f3d52022-10-12 10:02:33 +0200402 const char *all_features[] = {"*", NULL};
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100403 char *dir = NULL, *module = NULL;
404
aPiecek912f3d52022-10-12 10:02:33 +0200405 /* Create libyang context. */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100406 if (c->yang_lib_file) {
407 /* ignore features */
408 ly_set_erase(&c->schema_features, free_features);
409
aPiecek912f3d52022-10-12 10:02:33 +0200410 if (ly_ctx_new_ylpath(c->searchpaths, c->yang_lib_file, LYD_UNKNOWN, c->ctx_options, &c->ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -0400411 YLMSG_E("Unable to modify libyang context with yang-library data.\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100412 return -1;
413 }
414 } else {
415 /* set imp feature flag if all should be enabled */
aPiecek912f3d52022-10-12 10:02:33 +0200416 c->ctx_options |= !c->schema_features.count ? LY_CTX_ENABLE_IMP_FEATURES : 0;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100417
aPiecek912f3d52022-10-12 10:02:33 +0200418 if (ly_ctx_new(c->searchpaths, c->ctx_options, &c->ctx)) {
419 YLMSG_E("Unable to create libyang context\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100420 return -1;
421 }
aPiecek912f3d52022-10-12 10:02:33 +0200422 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100423
aPiecek912f3d52022-10-12 10:02:33 +0200424 /* set callback providing run-time extension instance data */
425 if (c->schema_context_filename) {
426 ly_ctx_set_ext_data_clb(c->ctx, ext_data_clb, c->schema_context_filename);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100427 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100428
Michal Vasko3f08fb92022-04-21 09:52:35 +0200429 /* process the operational and/or reply RPC content if any */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100430 if (c->data_operational.path) {
431 if (get_input(c->data_operational.path, NULL, &c->data_operational.format, &c->data_operational.in)) {
432 return -1;
433 }
434 }
Michal Vasko3f08fb92022-04-21 09:52:35 +0200435 if (c->reply_rpc.path) {
436 if (get_input(c->reply_rpc.path, NULL, &c->reply_rpc.format, &c->reply_rpc.in)) {
437 return -1;
438 }
439 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100440
441 for (int i = 0; i < argc - optind; i++) {
442 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
443 LYD_FORMAT format_data = LYD_UNKNOWN;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100444
445 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100446 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100447 }
448
449 if (format_schema) {
450 LY_ERR ret;
451 uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */
Michal Vasko59740872021-11-22 10:01:34 +0100452 const char **features;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100453
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100454 /* parse the input */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100455 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100456 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100457 }
458
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100459 if (c->yang_lib_file) {
460 /* just get the module, it should already be parsed */
461 mod = ly_ctx_get_module_implemented(c->ctx, module);
462 if (!mod) {
463 YLMSG_E("Schema module \"%s\" not implemented in yang-library data.\n", module);
464 goto error;
465 }
Michal Vasko59740872021-11-22 10:01:34 +0100466 } else {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100467 /* add temporarily also the path of the module itself */
468 if (ly_ctx_set_searchdir(c->ctx, dir) == LY_EEXIST) {
469 path_unset = 0;
470 }
471
472 /* get features list for this module */
473 if (!c->schema_features.count) {
474 features = all_features;
475 } else {
476 get_features(&c->schema_features, module, &features);
477 }
478
479 /* parse module */
480 ret = lys_parse(c->ctx, in, format_schema, features, &mod);
481 ly_ctx_unset_searchdir_last(c->ctx, path_unset);
482 if (ret) {
483 YLMSG_E("Parsing schema module \"%s\" failed.\n", argv[optind + i]);
484 goto error;
485 }
Michal Vasko703370c2021-10-19 14:07:16 +0200486 }
487
Radek Krejcie9f13b12020-11-09 17:42:04 +0100488 /* temporary cleanup */
489 free(dir);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100490 dir = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100491 free(module);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100492 module = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100493
roman300b8782022-08-11 12:49:21 +0200494 if (c->schema_out_format || c->feature_param_format) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100495 /* module will be printed */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100496 if (ly_set_add(&c->schema_modules, (void *)mod, 1, NULL)) {
497 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", argv[optind + i]);
Radek Krejcif2afd672020-11-26 12:29:23 +0100498 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100499 }
500 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100501 } else if (format_data) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100502 if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100503 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100504 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100505 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100506 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100507
508 ly_in_free(in, 1);
509 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100510 }
511
Michal Vaskoc91fb122021-11-23 09:00:17 +0100512 /* check that all specified features were applied, apply now if possible */
513 while ((sf = get_features_not_applied(&c->schema_features))) {
514 /* try to find implemented or the latest revision of this module */
515 mod = ly_ctx_get_module_implemented(c->ctx, sf->mod_name);
516 if (!mod) {
517 mod = ly_ctx_get_module_latest(c->ctx, sf->mod_name);
Michal Vasko686d8fc2021-11-22 10:03:23 +0100518 }
Michal Vaskoc91fb122021-11-23 09:00:17 +0100519 if (!mod) {
520 YLMSG_E("Specified features not applied, module \"%s\" not loaded.\n", sf->mod_name);
521 goto error;
522 }
523
524 /* we have the module, implement it if needed and enable the specific features */
525 if (lys_set_implemented(mod, (const char **)sf->features)) {
526 YLMSG_E("Implementing module \"%s\" failed.\n", mod->name);
527 goto error;
528 }
529 sf->applied = 1;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100530 }
531
Radek Krejcie9f13b12020-11-09 17:42:04 +0100532 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100533
534error:
535 ly_in_free(in, 1);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100536 free(dir);
537 free(module);
Radek Krejcif2afd672020-11-26 12:29:23 +0100538 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100539}
540
541/**
542 * @brief Process command line options and store the settings into the context.
543 *
544 * return -1 in case of error;
545 * return 0 in case of success and ready to process
546 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200547 */
548static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100549fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200550{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100551 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200552
Radek Krejcie9f13b12020-11-09 17:42:04 +0100553 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200554 struct option options[] = {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100555 {"help", no_argument, NULL, 'h'},
556 {"version", no_argument, NULL, 'v'},
557 {"verbose", no_argument, NULL, 'V'},
558 {"quiet", no_argument, NULL, 'Q'},
559 {"format", required_argument, NULL, 'f'},
560 {"path", required_argument, NULL, 'p'},
Michal Vaskod8b0e772022-03-18 13:36:04 +0100561 {"disable-searchdir", no_argument, NULL, 'D'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100562 {"features", required_argument, NULL, 'F'},
563 {"make-implemented", no_argument, NULL, 'i'},
564 {"schema-node", required_argument, NULL, 'P'},
565 {"single-node", no_argument, NULL, 'q'},
566 {"submodule", required_argument, NULL, 's'},
ekinzie0ab8b302022-10-10 03:03:57 -0400567 {"ext-data", required_argument, NULL, 'x'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100568 {"not-strict", no_argument, NULL, 'n'},
569 {"present", no_argument, NULL, 'e'},
570 {"type", required_argument, NULL, 't'},
571 {"default", required_argument, NULL, 'd'},
aPiecek1b629762023-04-12 13:52:30 +0200572 {"data-xpath", required_argument, NULL, 'E'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100573 {"list", no_argument, NULL, 'l'},
574 {"tree-line-length", required_argument, NULL, 'L'},
575 {"output", required_argument, NULL, 'o'},
576 {"operational", required_argument, NULL, 'O'},
Michal Vasko3f08fb92022-04-21 09:52:35 +0200577 {"reply-rpc", required_argument, NULL, 'R'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100578 {"merge", no_argument, NULL, 'm'},
579 {"yang-library", no_argument, NULL, 'y'},
580 {"yang-library-file", required_argument, NULL, 'Y'},
stewegd8e2fc92023-05-31 09:52:56 +0200581 {"extended-leafref", no_argument, NULL, 'X'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200582#ifndef NDEBUG
583 {"debug", required_argument, NULL, 'G'},
584#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200585 {NULL, 0, NULL, 0}
586 };
Radek Krejcie9f13b12020-11-09 17:42:04 +0100587 uint8_t data_type_set = 0;
aPiecek7941e322023-05-10 15:44:41 +0200588 uint32_t temp_lo = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100589
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100590 c->ctx_options = YL_DEFAULT_CTX_OPTIONS;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100591 c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
Michal Vasko05eaf832023-02-10 09:21:46 +0100592 c->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
aPiecek2cfeeae2021-04-21 13:17:34 +0200593 c->line_length = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100594
Michal Vasko27a4acb2021-11-22 10:03:46 +0100595 opterr = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200596#ifndef NDEBUG
aPiecek1b629762023-04-12 13:52:30 +0200597 while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:G:", options, &opt_index)) != -1)
Michal Vasko27a4acb2021-11-22 10:03:46 +0100598#else
aPiecek1b629762023-04-12 13:52:30 +0200599 while ((opt = getopt_long(argc, argv, "hvVQf:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:", options, &opt_index)) != -1)
Radek Krejcied5acc52019-04-25 15:57:04 +0200600#endif
Michal Vasko03fe6202022-07-22 16:23:13 +0200601 {
Radek Krejcied5acc52019-04-25 15:57:04 +0200602 switch (opt) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200603 case 'h': /* --help */
604 help(0);
605 return 1;
606
607 case 'v': /* --version */
608 version();
609 return 1;
610
611 case 'V': { /* --verbose */
612 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100613
Michal Vaskoc40548b2021-09-30 13:05:47 +0200614 if (verbosity < LY_LLDBG) {
Michal Vaskob0d307b2021-11-25 11:15:16 +0100615 ++verbosity;
Radek Krejcied5acc52019-04-25 15:57:04 +0200616 }
Michal Vaskob0d307b2021-11-25 11:15:16 +0100617 ly_log_level(verbosity);
Radek Krejcied5acc52019-04-25 15:57:04 +0200618 break;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200619 } /* case 'V' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100620
Michal Vaskob0d307b2021-11-25 11:15:16 +0100621 case 'Q': { /* --quiet */
622 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100623
Michal Vaskob0d307b2021-11-25 11:15:16 +0100624 if (verbosity == LY_LLERR) {
625 /* turn logging off */
626 ly_log_options(LY_LOSTORE_LAST);
627 } else if (verbosity > LY_LLERR) {
628 --verbosity;
629 }
630 ly_log_level(verbosity);
631 break;
632 } /* case 'Q' */
633
Radek Krejcie9f13b12020-11-09 17:42:04 +0100634 case 'f': /* --format */
635 if (!strcasecmp(optarg, "yang")) {
636 c->schema_out_format = LYS_OUT_YANG;
637 c->data_out_format = 0;
638 } else if (!strcasecmp(optarg, "yin")) {
639 c->schema_out_format = LYS_OUT_YIN;
640 c->data_out_format = 0;
641 } else if (!strcasecmp(optarg, "info")) {
642 c->schema_out_format = LYS_OUT_YANG_COMPILED;
643 c->data_out_format = 0;
644 } else if (!strcasecmp(optarg, "tree")) {
645 c->schema_out_format = LYS_OUT_TREE;
646 c->data_out_format = 0;
647 } else if (!strcasecmp(optarg, "xml")) {
648 c->schema_out_format = 0;
649 c->data_out_format = LYD_XML;
650 } else if (!strcasecmp(optarg, "json")) {
651 c->schema_out_format = 0;
652 c->data_out_format = LYD_JSON;
Michal Vaskod8b0e772022-03-18 13:36:04 +0100653 } else if (!strcasecmp(optarg, "lyb")) {
654 c->schema_out_format = 0;
655 c->data_out_format = LYD_LYB;
roman300b8782022-08-11 12:49:21 +0200656 } else if (!strcasecmp(optarg, "feature-param")) {
657 c->feature_param_format = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200658 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100659 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200660 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100661 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200662 }
663 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100664
Michal Vaskoc40548b2021-09-30 13:05:47 +0200665 case 'p': { /* --path */
666 struct stat st;
667
668 if (stat(optarg, &st) == -1) {
669 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
670 return -1;
671 }
672 if (!S_ISDIR(st.st_mode)) {
673 YLMSG_E("Provided search path is not a directory.\n");
674 return -1;
675 }
676
aPiecek912f3d52022-10-12 10:02:33 +0200677 if (searchpath_strcat(&c->searchpaths, optarg)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200678 YLMSG_E("Storing searchpath failed.\n");
679 return -1;
680 }
681
682 break;
683 } /* case 'p' */
684
685 case 'D': /* --disable-search */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100686 if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200687 YLMSG_W("The -D option specified too many times.\n");
688 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100689 if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {
690 c->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
691 c->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200692 } else {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100693 c->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200694 }
695 break;
696
697 case 'F': /* --features */
698 if (parse_features(optarg, &c->schema_features)) {
699 return -1;
700 }
701 break;
702
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200703 case 'i': /* --make-implemented */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100704 if (c->ctx_options & LY_CTX_REF_IMPLEMENTED) {
705 c->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
706 c->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200707 } else {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100708 c->ctx_options |= LY_CTX_REF_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200709 }
710 break;
711
Radek Krejcie9f13b12020-11-09 17:42:04 +0100712 case 'P': /* --schema-node */
713 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200714 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100715
716 case 'q': /* --single-node */
717 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
718 break;
719
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200720 case 's': /* --submodule */
721 c->submodule = optarg;
722 break;
723
ekinzie0ab8b302022-10-10 03:03:57 -0400724 case 'x': /* --ext-data */
725 c->schema_context_filename = strdup(optarg);
726 break;
727
Michal Vasko667ce6b2021-01-25 15:00:27 +0100728 case 'n': /* --not-strict */
729 c->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100730 break;
731
732 case 'e': /* --present */
733 c->data_validate_options |= LYD_VALIDATE_PRESENT;
734 break;
735
736 case 't': /* --type */
737 if (data_type_set) {
738 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
739 return -1;
740 }
741
742 if (!strcasecmp(optarg, "config")) {
743 c->data_parse_options |= LYD_PARSE_NO_STATE;
Michal Vaskof6bda332021-02-01 08:59:03 +0100744 c->data_validate_options |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100745 } else if (!strcasecmp(optarg, "get")) {
746 c->data_parse_options |= LYD_PARSE_ONLY;
aPiecekbe6c0602023-04-18 11:29:09 +0200747 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100748 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200749 } else if (!strcasecmp(optarg, "rpc")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100750 c->data_type = LYD_TYPE_RPC_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200751 } else if (!strcasecmp(optarg, "nc-rpc")) {
752 c->data_type = LYD_TYPE_RPC_NETCONF;
753 } else if (!strcasecmp(optarg, "reply")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100754 c->data_type = LYD_TYPE_REPLY_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200755 } else if (!strcasecmp(optarg, "nc-reply")) {
756 c->data_type = LYD_TYPE_REPLY_NETCONF;
757 } else if (!strcasecmp(optarg, "notif")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100758 c->data_type = LYD_TYPE_NOTIF_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200759 } else if (!strcasecmp(optarg, "nc-notif")) {
760 c->data_type = LYD_TYPE_NOTIF_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100761 } else if (!strcasecmp(optarg, "data")) {
762 /* default option */
763 } else {
764 YLMSG_E("Unknown data tree type %s\n", optarg);
765 help(1);
766 return -1;
767 }
768
769 data_type_set = 1;
770 break;
771
Michal Vaskoc40548b2021-09-30 13:05:47 +0200772 case 'd': /* --default */
773 if (!strcasecmp(optarg, "all")) {
774 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
775 } else if (!strcasecmp(optarg, "all-tagged")) {
776 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
777 } else if (!strcasecmp(optarg, "trim")) {
778 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
779 } else if (!strcasecmp(optarg, "implicit-tagged")) {
780 c->data_print_options = (c->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
781 } else {
782 YLMSG_E("Unknown default mode %s\n", optarg);
783 help(1);
784 return -1;
785 }
786 break;
787
aPiecek1b629762023-04-12 13:52:30 +0200788 case 'E': /* --data-xpath */
789 if (ly_set_add(&c->data_xpath, optarg, 0, NULL)) {
790 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
791 return -1;
792 }
793 break;
794
Michal Vaskoc40548b2021-09-30 13:05:47 +0200795 case 'l': /* --list */
796 c->list = 1;
797 break;
798
799 case 'L': /* --tree-line-length */
800 c->line_length = atoi(optarg);
801 break;
802
803 case 'o': /* --output */
804 if (c->out) {
805 YLMSG_E("Only a single output can be specified.\n");
806 return -1;
807 } else {
808 if (ly_out_new_filepath(optarg, &c->out)) {
809 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
810 return -1;
811 }
812 }
813 break;
814
Radek Krejcie9f13b12020-11-09 17:42:04 +0100815 case 'O': /* --operational */
816 if (c->data_operational.path) {
817 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
818 return -1;
819 }
820 c->data_operational.path = optarg;
821 break;
822
Michal Vasko3f08fb92022-04-21 09:52:35 +0200823 case 'R': /* --reply-rpc */
824 if (c->reply_rpc.path) {
825 YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n");
826 return -1;
827 }
828 c->reply_rpc.path = optarg;
829 break;
830
Radek Krejcie9f13b12020-11-09 17:42:04 +0100831 case 'm': /* --merge */
832 c->data_merge = 1;
833 break;
834
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100835 case 'y': /* --yang-library */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100836 c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
837 break;
838
839 case 'Y': /* --yang-library-file */
840 c->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
841 c->yang_lib_file = optarg;
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100842 break;
843
stewegd8e2fc92023-05-31 09:52:56 +0200844 case 'X': /* --extended-leafref */
845 c->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
846 break;
847
Radek Krejcied5acc52019-04-25 15:57:04 +0200848#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100849 case 'G': { /* --debug */
850 uint32_t dbg_groups = 0;
851 const char *ptr = optarg;
852
Radek Krejcied5acc52019-04-25 15:57:04 +0200853 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100854 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100855 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100856 ptr += sizeof "dict" - 1;
857 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100858 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100859 ptr += sizeof "xpath" - 1;
Michal Vasko27a4acb2021-11-22 10:03:46 +0100860 } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {
861 dbg_groups |= LY_LDGDEPSETS;
862 ptr += sizeof "dep-sets" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200863 }
864
865 if (ptr[0]) {
866 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100867 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
868 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200869 }
870 ++ptr;
871 }
872 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100873 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200874 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100875 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200876#endif
Michal Vasko27a4acb2021-11-22 10:03:46 +0100877 default:
878 YLMSG_E("Invalid option or missing argument: -%c\n", optopt);
879 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100880 } /* switch */
881 }
882
Radek Krejcie9f13b12020-11-09 17:42:04 +0100883 /* additional checks for the options combinations */
884 if (!c->list && (optind >= argc)) {
885 help(1);
886 YLMSG_E("Missing <schema> to process.\n");
887 return 1;
888 }
889
aPiecek1b629762023-04-12 13:52:30 +0200890 if (c->data_xpath.count) {
891 c->data_merge = 1;
892 }
893 if (c->data_xpath.count && (c->schema_out_format || c->data_out_format)) {
894 YLMSG_E("The --format option cannot be combined with --data-xpath option.\n");
895 return -1;
896 }
897 if (c->data_xpath.count && (c->data_print_options & LYD_PRINT_WD_MASK)) {
898 YLMSG_E("The --default option cannot be combined with --data-xpath option.\n");
899 return -1;
900 }
901
Radek Krejcie9f13b12020-11-09 17:42:04 +0100902 if (c->data_merge) {
903 if (c->data_type || (c->data_parse_options & LYD_PARSE_ONLY)) {
904 /* switch off the option, incompatible input data type */
aPiecek3d46fa92023-05-10 08:30:39 +0200905 YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100906 c->data_merge = 0;
907 } else {
908 /* postpone validation after the merge of all the input data */
909 c->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200910 }
911 }
912
Radek Krejcie9f13b12020-11-09 17:42:04 +0100913 if (c->data_operational.path && !c->data_type) {
aPiecekce481582023-04-26 15:44:59 +0200914 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100915 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200916 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100917
Michal Vasko3f08fb92022-04-21 09:52:35 +0200918 if (c->reply_rpc.path && (c->data_type != LYD_TYPE_REPLY_NETCONF)) {
aPiecekce481582023-04-26 15:44:59 +0200919 YLMSG_W("Source RPC is needed only for NETCONF Reply input data type.\n");
Michal Vasko3f08fb92022-04-21 09:52:35 +0200920 c->data_operational.path = NULL;
921 } else if (!c->reply_rpc.path && (c->data_type == LYD_TYPE_REPLY_NETCONF)) {
922 YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\n");
923 return -1;
924 }
925
aPiecek2cfeeae2021-04-21 13:17:34 +0200926 if ((c->schema_out_format != LYS_OUT_TREE) && c->line_length) {
aPiecekce481582023-04-26 15:44:59 +0200927 YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n");
aPiecek2cfeeae2021-04-21 13:17:34 +0200928 }
929
Radek Krejcie9f13b12020-11-09 17:42:04 +0100930 /* default output stream */
931 if (!c->out) {
932 if (ly_out_new_file(stdout, &c->out)) {
933 YLMSG_E("Unable to set stdout as output.\n");
934 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200935 }
936 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100937
aPiecek7ac66d52021-05-20 07:54:07 +0200938 if (c->schema_out_format == LYS_OUT_TREE) {
939 /* print tree from lysc_nodes */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100940 c->ctx_options |= LY_CTX_SET_PRIV_PARSED;
aPiecek7ac66d52021-05-20 07:54:07 +0200941 }
942
Radek Krejcie9f13b12020-11-09 17:42:04 +0100943 /* process input files provided as standalone command line arguments,
944 * schema modules are parsed and inserted into the context,
945 * data files are just checked and prepared into internal structures for further processing */
946 ret = fill_context_inputs(argc, argv, c);
947 if (ret) {
948 return ret;
949 }
950
951 /* the second batch of checks */
952 if (c->schema_print_options && !c->schema_out_format) {
953 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
954 }
955 if (c->schema_parse_options && !c->schema_modules.count) {
956 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
957 }
958 if (c->data_print_options && !c->data_out_format) {
959 YLMSG_W("data printer options specified, but the data output format is missing.\n");
960 }
Michal Vasko667ce6b2021-01-25 15:00:27 +0100961 if (((c->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || c->data_type) && !c->data_inputs.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100962 YLMSG_W("Data parser options specified, but no data input file provided.\n");
963 }
964
965 if (c->schema_node_path) {
aPiecek7941e322023-05-10 15:44:41 +0200966 /* turn off logging so that the message is not repeated */
967 ly_temp_log_options(&temp_lo);
968 /* search operation input */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100969 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
970 if (!c->schema_node) {
aPiecek7941e322023-05-10 15:44:41 +0200971 /* restore logging so an error may be displayed */
972 ly_temp_log_options(NULL);
973 /* search operation output */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100974 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100975 if (!c->schema_node) {
976 YLMSG_E("Invalid schema path.\n");
977 return -1;
978 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200979 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200980 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100981
Radek Krejcie9f13b12020-11-09 17:42:04 +0100982 return 0;
983}
984
985int
986main_ni(int argc, char *argv[])
987{
988 int ret = EXIT_SUCCESS, r;
989 struct context c = {0};
roman300b8782022-08-11 12:49:21 +0200990 char *features_output = NULL;
991 struct ly_set set = {0};
992 uint32_t u;
Radek Krejcied5acc52019-04-25 15:57:04 +0200993
994 /* set callback for printing libyang messages */
995 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100996
997 r = fill_context(argc, argv, &c);
998 if (r < 0) {
999 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +02001000 }
Radek Krejcie9f13b12020-11-09 17:42:04 +01001001 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +02001002 goto cleanup;
1003 }
1004
Radek Krejcie9f13b12020-11-09 17:42:04 +01001005 /* do the required job - parse, validate, print */
1006
1007 if (c.list) {
1008 /* print the list of schemas */
aPiecek34262622023-03-27 08:28:25 +02001009 ret = print_list(c.out, c.ctx, c.data_out_format);
1010 goto cleanup;
Radek Krejci047d64e2020-12-03 12:57:10 +01001011 } else {
roman300b8782022-08-11 12:49:21 +02001012 if (c.feature_param_format) {
1013 for (u = 0; u < c.schema_modules.count; u++) {
1014 if (collect_features(c.schema_modules.objs[u], &set)) {
1015 YLMSG_E("Unable to read features from a module.\n");
1016 goto cleanup;
1017 }
1018 if (generate_features_output(c.schema_modules.objs[u], &set, &features_output)) {
1019 YLMSG_E("Unable to generate feature command output.\n");
1020 goto cleanup;
1021 }
1022 ly_set_erase(&set, NULL);
1023 }
1024 ly_print(c.out, "%s\n", features_output);
1025 } else if (c.schema_out_format) {
Radek Krejci047d64e2020-12-03 12:57:10 +01001026 if (c.schema_node) {
aPiecek146fd192023-03-30 11:22:16 +02001027 ret = lys_print_node(c.out, c.schema_node, c.schema_out_format, c.line_length, c.schema_print_options);
Radek Krejcie9f13b12020-11-09 17:42:04 +01001028 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +01001029 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +01001030 goto cleanup;
1031 }
Michal Vaskofe74d1e2021-09-30 13:17:36 +02001032 } else if (c.submodule) {
1033 const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(c.ctx, c.submodule);
Michal Vasko26bbb272022-08-02 14:54:33 +02001034
Michal Vaskofe74d1e2021-09-30 13:17:36 +02001035 if (!submod) {
1036 YLMSG_E("Unable to find submodule %s.\n", c.submodule);
1037 goto cleanup;
1038 }
1039
1040 ret = lys_print_submodule(c.out, submod, c.schema_out_format, c.line_length, c.schema_print_options);
1041 if (ret) {
1042 YLMSG_E("Unable to print submodule %s.\n", submod->name);
1043 goto cleanup;
1044 }
Radek Krejci047d64e2020-12-03 12:57:10 +01001045 } else {
roman300b8782022-08-11 12:49:21 +02001046 for (u = 0; u < c.schema_modules.count; ++u) {
aPiecek2cfeeae2021-04-21 13:17:34 +02001047 ret = lys_print_module(c.out, (struct lys_module *)c.schema_modules.objs[u], c.schema_out_format,
1048 c.line_length, c.schema_print_options);
aPiecek8f0b7882021-05-21 10:18:36 +02001049 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
1050 if ((c.schema_out_format == LYS_OUT_TREE) && (u + 1 < c.schema_modules.count)) {
1051 ly_print(c.out, "\n");
1052 }
Radek Krejci047d64e2020-12-03 12:57:10 +01001053 if (ret) {
1054 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)c.schema_modules.objs[u])->name);
1055 goto cleanup;
1056 }
1057 }
Radek Krejcie9f13b12020-11-09 17:42:04 +01001058 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001059 }
Radek Krejci047d64e2020-12-03 12:57:10 +01001060
1061 /* do the data validation despite the schema was printed */
1062 if (c.data_inputs.size) {
Michal Vasko0f1e4562022-03-29 11:57:57 +02001063 ret = process_data(c.ctx, c.data_type, c.data_merge, c.data_out_format, c.out, c.data_parse_options,
aPiecek1b629762023-04-12 13:52:30 +02001064 c.data_validate_options, c.data_print_options, &c.data_operational, &c.reply_rpc, &c.data_inputs,
1065 &c.data_xpath);
Michal Vasko0f1e4562022-03-29 11:57:57 +02001066 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +01001067 goto cleanup;
1068 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001069 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001070 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001071
1072cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +01001073 /* cleanup */
1074 erase_context(&c);
roman300b8782022-08-11 12:49:21 +02001075 free(features_output);
1076 ly_set_erase(&set, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +02001077
Radek Krejcied5acc52019-04-25 15:57:04 +02001078 return ret;
1079}