blob: b836ff135affd517cd678405a17fc6196755678e [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"
28
Radek Krejcie9f13b12020-11-09 17:42:04 +010029#include "common.h"
aPiecek8f0b7882021-05-21 10:18:36 +020030#include "out.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tools/config.h"
32
Radek Krejcie9f13b12020-11-09 17:42:04 +010033/**
34 * @brief Context structure to hold and pass variables in a structured form.
35 */
36struct context {
37 /* libyang context for the run */
Michal Vasko24aaf8e2022-02-04 11:15:26 +010038 const char *yang_lib_file;
39 uint16_t ctx_options;
Radek Krejcie9f13b12020-11-09 17:42:04 +010040 struct ly_ctx *ctx;
Radek Krejci535ea9f2020-05-29 16:01:05 +020041
Radek Krejcie9f13b12020-11-09 17:42:04 +010042 /* prepared output (--output option or stdout by default) */
43 struct ly_out *out;
Radek Krejci535ea9f2020-05-29 16:01:05 +020044
aPiecek912f3d52022-10-12 10:02:33 +020045 char *searchpaths;
Radek Krejcied5acc52019-04-25 15:57:04 +020046
Radek Krejcie9f13b12020-11-09 17:42:04 +010047 /* options flags */
48 uint8_t list; /* -l option to print list of schemas */
Radek Krejcied5acc52019-04-25 15:57:04 +020049
aPiecek2cfeeae2021-04-21 13:17:34 +020050 /* line length for 'tree' format */
51 size_t line_length; /* --tree-line-length */
52
Radek Krejcie9f13b12020-11-09 17:42:04 +010053 /*
54 * schema
55 */
Michal Vaskoa9a98612021-11-22 10:00:27 +010056 /* set schema modules' features via --features option (struct schema_features *) */
Radek Krejcie9f13b12020-11-09 17:42:04 +010057 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 Vaskofe74d1e2021-09-30 13:17:36 +020069 const char *submodule;
Radek Krejcie9f13b12020-11-09 17:42:04 +010070
ekinzie0ab8b302022-10-10 03:03:57 -040071 /* 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 Krejcie9f13b12020-11-09 17:42:04 +010077 /* value of --format in case of schema format */
78 LYS_OUTFORMAT schema_out_format;
roman300b8782022-08-11 12:49:21 +020079 ly_bool feature_param_format;
Radek Krejcie9f13b12020-11-09 17:42:04 +010080
81 /*
82 * data
83 */
84 /* various options based on --type option */
Michal Vaskoe0665742021-02-11 11:08:44 +010085 enum lyd_type data_type;
Radek Krejcie9f13b12020-11-09 17:42:04 +010086 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
aPiecekef0a3392023-05-16 10:34:32 +020096 /* value of --in-format in case of data format */
97 LYD_FORMAT data_in_format;
98
Radek Krejcie9f13b12020-11-09 17:42:04 +010099 /* input data files (struct cmdline_file *) */
100 struct ly_set data_inputs;
101
Radek Krejcie9f13b12020-11-09 17:42:04 +0100102 /* storage for --operational */
103 struct cmdline_file data_operational;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200104
105 /* storage for --reply-rpc */
106 struct cmdline_file reply_rpc;
aPiecek1b629762023-04-12 13:52:30 +0200107
108 /* storage for --data-xpath */
109 struct ly_set data_xpath;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100110};
111
112static void
113erase_context(struct context *c)
114{
115 /* data */
116 ly_set_erase(&c->data_inputs, free_cmdline_file);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100117 ly_in_free(c->data_operational.in, 1);
aPiecek1b629762023-04-12 13:52:30 +0200118 ly_set_erase(&c->data_xpath, NULL);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100119
120 /* schema */
121 ly_set_erase(&c->schema_features, free_features);
122 ly_set_erase(&c->schema_modules, NULL);
123
124 /* context */
aPiecek912f3d52022-10-12 10:02:33 +0200125 free(c->searchpaths);
126 c->searchpaths = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100127
aPiecekadb01c42023-06-02 08:28:52 +0200128 /* --reply-rpc */
129 ly_in_free(c->reply_rpc.in, 1);
130
Radek Krejcie9f13b12020-11-09 17:42:04 +0100131 ly_out_free(c->out, NULL, 0);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200132 ly_ctx_destroy(c->ctx);
ekinzie0ab8b302022-10-10 03:03:57 -0400133
134 if (c->schema_context_filename) {
135 free(c->schema_context_filename);
136 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100137}
138
139static void
140version(void)
141{
142 printf("yanglint %s\n", PROJECT_VERSION);
143}
144
145static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200146help(int shortout)
147{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100148
Michal Vasko3f08fb92022-04-21 09:52:35 +0200149 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"
aPieceka40764b2023-04-27 15:34:56 +0200158 " <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 Vasko3f08fb92022-04-21 09:52:35 +0200160 " 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 Krejcie9f13b12020-11-09 17:42:04 +0100163 " yanglint\n"
164 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200165
166 if (shortout) {
167 return;
168 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100169 printf("Options:\n"
170 " -h, --help Show this help message and exit.\n"
171 " -v, --version Show version number and exit.\n"
Michal Vaskob0d307b2021-11-25 11:15:16 +0100172 " -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 Krejcie9f13b12020-11-09 17:42:04 +0100176
Michal Vaskoc40548b2021-09-30 13:05:47 +0200177 printf(" -f FORMAT, --format=FORMAT\n"
178 " Convert input into FORMAT. Supported formats: \n"
roman300b8782022-08-11 12:49:21 +0200179 " yang, yin, tree, info and feature-param for schemas,\n"
Michal Vaskod8b0e772022-03-18 13:36:04 +0100180 " xml, json, and lyb for data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200181
aPiecekef0a3392023-05-16 10:34:32 +0200182 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 Vaskoc40548b2021-09-30 13:05:47 +0200187 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"
aPiecek4f306da2023-03-27 09:06:07 +0200190 " path of the module being added is used implicitly. Subdirectories\n"
191 " are also searched\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100192
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200193 printf(" -D, --disable-searchdir\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100194 " 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 Vasko22e0f3a2021-09-22 12:19:23 +0200197 " explicitly specified).\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100198
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200199 printf(" -F FEATURES, --features=FEATURES\n"
Michal Vasko59740872021-11-22 10:01:34 +0100200 " 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 Krejcie9f13b12020-11-09 17:42:04 +0100205
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200206 printf(" -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100207 " Make the imported modules \"referenced\" from any loaded\n"
208 " module also implemented. If specified a second time, all the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200209 " modules are set implemented.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100210
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200211 printf(" -P PATH, --schema-node=PATH\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100212 " 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 Vasko22e0f3a2021-09-22 12:19:23 +0200218 " only about a single node specified as PATH argument.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100219
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200220 printf(" -s SUBMODULE, --submodule=SUBMODULE\n"
221 " Print the specific submodule instead of the main module.\n\n");
222
ekinzie0ab8b302022-10-10 03:03:57 -0400223 printf(" -x FILE, --ext-data=FILE\n"
224 " File containing the specific data required by an extension. Required by\n"
Michal Vasko14662802022-11-08 08:45:46 +0100225 " the schema-mount extension, for example, when the operational data are\n"
ekinzie0ab8b302022-10-10 03:03:57 -0400226 " expected in the file. File format is guessed.\n\n");
227
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200228 printf(" -n, --not-strict\n"
Michal Vasko667ce6b2021-01-25 15:00:27 +0100229 " Do not require strict data parsing (silently skip unknown data),\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200230 " has no effect for schemas.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100231
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200232 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100233 " 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 Vasko22e0f3a2021-09-22 12:19:23 +0200236 " provided input data files.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100237
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200238 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100239 " 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 Vasko3f08fb92022-04-21 09:52:35 +0200242 " 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 Vaskoc40548b2021-09-30 13:05:47 +0200259
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
aPiecek1b629762023-04-12 13:52:30 +0200270 printf(" -E XPATH, --data-xpath=XPATH\n"
aPiecek41955272023-05-10 16:10:17 +0200271 " Evaluate XPATH expression over the data and print the nodes satisfying\n"
aPiecek1b629762023-04-12 13:52:30 +0200272 " 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 Vaskoc40548b2021-09-30 13:05:47 +0200277 printf(" -l, --list Print info about the loaded schemas.\n"
278 " (i - imported module, I - implemented module)\n"
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100279 " In case the '-f' option with data encoding is specified,\n"
280 " the list is printed as \"ietf-yang-library\" data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200281
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 Krejcie9f13b12020-11-09 17:42:04 +0100288
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200289 printf(" -O FILE, --operational=FILE\n"
Michal Vasko9e81ce52022-04-29 10:16:21 +0200290 " 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"
aPieceka40764b2023-04-27 15:34:56 +0200293 " In case of a nested notification or action, its parent existence\n"
294 " is also checked in these operational data.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100295
Michal Vasko3f08fb92022-04-21 09:52:35 +0200296 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 Vasko22e0f3a2021-09-22 12:19:23 +0200300 printf(" -m, --merge Merge input data files into a single tree and validate at\n"
Michal Vasko06158fb2022-03-29 12:13:16 +0200301 " once. The option has effect only for 'data' and 'config' TYPEs.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100302
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200303 printf(" -y, --yang-library\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100304 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
305 " Note that this module includes definitions of mandatory state\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200306 " data that can result in unexpected data validation errors.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200307
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100308 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
stewegd8e2fc92023-05-31 09:52:56 +0200313 printf(" -X, --extended-leafref\n"
314 " Allow usage of deref() XPath function within leafref\n\n");
315
Michal Vaskoc40548b2021-09-30 13:05:47 +0200316#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 Vasko27a4acb2021-11-22 10:03:46 +0100320 " <group>[,<group>]* (dict, xpath, dep-sets)\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200321#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200322}
323
Radek Krejcie9f13b12020-11-09 17:42:04 +0100324static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200325libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
326{
327 char *levstr;
328
Radek Krejcie9f13b12020-11-09 17:42:04 +0100329 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 Krejcied5acc52019-04-25 15:57:04 +0200347 }
348}
349
Michal Vasko686d8fc2021-11-22 10:03:23 +0100350static struct schema_features *
351get_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 Vasko26bbb272022-08-02 14:54:33 +0200355
Michal Vasko686d8fc2021-11-22 10:03:23 +0100356 if (!sf->applied) {
357 return sf;
358 }
359 }
360
361 return NULL;
362}
363
Radek Krejcie9f13b12020-11-09 17:42:04 +0100364static int
365fill_context_inputs(int argc, char *argv[], struct context *c)
366{
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100367 struct ly_in *in = NULL;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100368 struct schema_features *sf;
Michal Vaskoc91fb122021-11-23 09:00:17 +0100369 struct lys_module *mod;
aPiecek912f3d52022-10-12 10:02:33 +0200370 const char *all_features[] = {"*", NULL};
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100371 char *dir = NULL, *module = NULL;
372
aPiecek912f3d52022-10-12 10:02:33 +0200373 /* Create libyang context. */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100374 if (c->yang_lib_file) {
375 /* ignore features */
376 ly_set_erase(&c->schema_features, free_features);
377
aPiecek912f3d52022-10-12 10:02:33 +0200378 if (ly_ctx_new_ylpath(c->searchpaths, c->yang_lib_file, LYD_UNKNOWN, c->ctx_options, &c->ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -0400379 YLMSG_E("Unable to modify libyang context with yang-library data.\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100380 return -1;
381 }
382 } else {
383 /* set imp feature flag if all should be enabled */
aPiecek912f3d52022-10-12 10:02:33 +0200384 c->ctx_options |= !c->schema_features.count ? LY_CTX_ENABLE_IMP_FEATURES : 0;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100385
aPiecek912f3d52022-10-12 10:02:33 +0200386 if (ly_ctx_new(c->searchpaths, c->ctx_options, &c->ctx)) {
387 YLMSG_E("Unable to create libyang context\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100388 return -1;
389 }
aPiecek912f3d52022-10-12 10:02:33 +0200390 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100391
aPiecek912f3d52022-10-12 10:02:33 +0200392 /* 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 Vasko24aaf8e2022-02-04 11:15:26 +0100395 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100396
Michal Vasko3f08fb92022-04-21 09:52:35 +0200397 /* process the operational and/or reply RPC content if any */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100398 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 Vasko3f08fb92022-04-21 09:52:35 +0200403 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 Krejcie9f13b12020-11-09 17:42:04 +0100408
409 for (int i = 0; i < argc - optind; i++) {
410 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
aPiecekef0a3392023-05-16 10:34:32 +0200411 LYD_FORMAT format_data = c->data_in_format;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100412
413 if (get_input(argv[optind + i], &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100414 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100415 }
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 Vasko59740872021-11-22 10:01:34 +0100420 const char **features;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100421
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100422 /* parse the input */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100423 if (parse_schema_path(argv[optind + i], &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100424 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100425 }
426
aPieceka6d45b52023-05-19 11:48:59 +0200427 mod = NULL;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100428 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);
aPieceka6d45b52023-05-19 11:48:59 +0200431 }
432 if (!mod) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100433 /* 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 Vasko703370c2021-10-19 14:07:16 +0200452 }
453
Radek Krejcie9f13b12020-11-09 17:42:04 +0100454 /* temporary cleanup */
455 free(dir);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100456 dir = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100457 free(module);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100458 module = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100459
roman300b8782022-08-11 12:49:21 +0200460 if (c->schema_out_format || c->feature_param_format) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100461 /* module will be printed */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100462 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 Krejcif2afd672020-11-26 12:29:23 +0100464 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100465 }
466 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100467 } else if (format_data) {
Radek Krejci6784a4e2020-12-09 14:23:05 +0100468 if (!fill_cmdline_file(&c->data_inputs, in, argv[optind + i], format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100469 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100470 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100471 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100472 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100473
474 ly_in_free(in, 1);
475 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100476 }
477
Michal Vaskoc91fb122021-11-23 09:00:17 +0100478 /* 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 Vasko686d8fc2021-11-22 10:03:23 +0100484 }
Michal Vaskoc91fb122021-11-23 09:00:17 +0100485 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 Vasko686d8fc2021-11-22 10:03:23 +0100496 }
497
Radek Krejcie9f13b12020-11-09 17:42:04 +0100498 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100499
500error:
501 ly_in_free(in, 1);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100502 free(dir);
503 free(module);
Radek Krejcif2afd672020-11-26 12:29:23 +0100504 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100505}
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 Krejcied5acc52019-04-25 15:57:04 +0200513 */
514static int
Radek Krejcie9f13b12020-11-09 17:42:04 +0100515fill_context(int argc, char *argv[], struct context *c)
Radek Krejcied5acc52019-04-25 15:57:04 +0200516{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100517 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200518
Radek Krejcie9f13b12020-11-09 17:42:04 +0100519 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200520 struct option options[] = {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100521 {"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 Vaskod8b0e772022-03-18 13:36:04 +0100527 {"disable-searchdir", no_argument, NULL, 'D'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100528 {"features", required_argument, NULL, 'F'},
529 {"make-implemented", no_argument, NULL, 'i'},
aPiecekef0a3392023-05-16 10:34:32 +0200530 {"in-format", required_argument, NULL, 'I'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100531 {"schema-node", required_argument, NULL, 'P'},
532 {"single-node", no_argument, NULL, 'q'},
533 {"submodule", required_argument, NULL, 's'},
ekinzie0ab8b302022-10-10 03:03:57 -0400534 {"ext-data", required_argument, NULL, 'x'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100535 {"not-strict", no_argument, NULL, 'n'},
536 {"present", no_argument, NULL, 'e'},
537 {"type", required_argument, NULL, 't'},
538 {"default", required_argument, NULL, 'd'},
aPiecek1b629762023-04-12 13:52:30 +0200539 {"data-xpath", required_argument, NULL, 'E'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100540 {"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 Vasko3f08fb92022-04-21 09:52:35 +0200544 {"reply-rpc", required_argument, NULL, 'R'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100545 {"merge", no_argument, NULL, 'm'},
546 {"yang-library", no_argument, NULL, 'y'},
547 {"yang-library-file", required_argument, NULL, 'Y'},
stewegd8e2fc92023-05-31 09:52:56 +0200548 {"extended-leafref", no_argument, NULL, 'X'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200549#ifndef NDEBUG
550 {"debug", required_argument, NULL, 'G'},
551#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200552 {NULL, 0, NULL, 0}
553 };
Radek Krejcie9f13b12020-11-09 17:42:04 +0100554 uint8_t data_type_set = 0;
aPiecek7941e322023-05-10 15:44:41 +0200555 uint32_t temp_lo = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100556
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100557 c->ctx_options = YL_DEFAULT_CTX_OPTIONS;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100558 c->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
Michal Vasko05eaf832023-02-10 09:21:46 +0100559 c->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
aPiecek2cfeeae2021-04-21 13:17:34 +0200560 c->line_length = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100561
Michal Vasko27a4acb2021-11-22 10:03:46 +0100562 opterr = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200563#ifndef NDEBUG
aPiecekef0a3392023-05-16 10:34:32 +0200564 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 Vasko27a4acb2021-11-22 10:03:46 +0100565#else
aPiecekef0a3392023-05-16 10:34:32 +0200566 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 Krejcied5acc52019-04-25 15:57:04 +0200567#endif
Michal Vasko03fe6202022-07-22 16:23:13 +0200568 {
Radek Krejcied5acc52019-04-25 15:57:04 +0200569 switch (opt) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200570 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 Vasko2bf4af42023-01-04 12:08:38 +0100580
Michal Vaskoc40548b2021-09-30 13:05:47 +0200581 if (verbosity < LY_LLDBG) {
Michal Vaskob0d307b2021-11-25 11:15:16 +0100582 ++verbosity;
Radek Krejcied5acc52019-04-25 15:57:04 +0200583 }
Michal Vaskob0d307b2021-11-25 11:15:16 +0100584 ly_log_level(verbosity);
Radek Krejcied5acc52019-04-25 15:57:04 +0200585 break;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200586 } /* case 'V' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100587
Michal Vaskob0d307b2021-11-25 11:15:16 +0100588 case 'Q': { /* --quiet */
589 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100590
Michal Vaskob0d307b2021-11-25 11:15:16 +0100591 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 Krejcie9f13b12020-11-09 17:42:04 +0100601 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 Vaskod8b0e772022-03-18 13:36:04 +0100620 } else if (!strcasecmp(optarg, "lyb")) {
621 c->schema_out_format = 0;
622 c->data_out_format = LYD_LYB;
roman300b8782022-08-11 12:49:21 +0200623 } else if (!strcasecmp(optarg, "feature-param")) {
624 c->feature_param_format = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200625 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100626 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200627 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100628 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200629 }
630 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100631
aPiecekef0a3392023-05-16 10:34:32 +0200632 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 Vaskoc40548b2021-09-30 13:05:47 +0200646 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
aPiecek912f3d52022-10-12 10:02:33 +0200658 if (searchpath_strcat(&c->searchpaths, optarg)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200659 YLMSG_E("Storing searchpath failed.\n");
660 return -1;
661 }
662
663 break;
664 } /* case 'p' */
665
666 case 'D': /* --disable-search */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100667 if (c->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200668 YLMSG_W("The -D option specified too many times.\n");
669 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100670 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 Vaskoc40548b2021-09-30 13:05:47 +0200673 } else {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100674 c->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200675 }
676 break;
677
678 case 'F': /* --features */
679 if (parse_features(optarg, &c->schema_features)) {
680 return -1;
681 }
682 break;
683
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200684 case 'i': /* --make-implemented */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100685 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 Vaskoc40548b2021-09-30 13:05:47 +0200688 } else {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100689 c->ctx_options |= LY_CTX_REF_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200690 }
691 break;
692
Radek Krejcie9f13b12020-11-09 17:42:04 +0100693 case 'P': /* --schema-node */
694 c->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200695 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100696
697 case 'q': /* --single-node */
698 c->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
699 break;
700
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200701 case 's': /* --submodule */
702 c->submodule = optarg;
703 break;
704
ekinzie0ab8b302022-10-10 03:03:57 -0400705 case 'x': /* --ext-data */
706 c->schema_context_filename = strdup(optarg);
707 break;
708
Michal Vasko667ce6b2021-01-25 15:00:27 +0100709 case 'n': /* --not-strict */
710 c->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100711 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 Vaskof6bda332021-02-01 08:59:03 +0100725 c->data_validate_options |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100726 } else if (!strcasecmp(optarg, "get")) {
727 c->data_parse_options |= LYD_PARSE_ONLY;
aPiecekbe6c0602023-04-18 11:29:09 +0200728 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100729 c->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200730 } else if (!strcasecmp(optarg, "rpc")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100731 c->data_type = LYD_TYPE_RPC_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200732 } else if (!strcasecmp(optarg, "nc-rpc")) {
733 c->data_type = LYD_TYPE_RPC_NETCONF;
734 } else if (!strcasecmp(optarg, "reply")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100735 c->data_type = LYD_TYPE_REPLY_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200736 } else if (!strcasecmp(optarg, "nc-reply")) {
737 c->data_type = LYD_TYPE_REPLY_NETCONF;
738 } else if (!strcasecmp(optarg, "notif")) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100739 c->data_type = LYD_TYPE_NOTIF_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200740 } else if (!strcasecmp(optarg, "nc-notif")) {
741 c->data_type = LYD_TYPE_NOTIF_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100742 } 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 Vaskoc40548b2021-09-30 13:05:47 +0200753 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
aPiecek1b629762023-04-12 13:52:30 +0200769 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 Vaskoc40548b2021-09-30 13:05:47 +0200776 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 Krejcie9f13b12020-11-09 17:42:04 +0100796 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 Vasko3f08fb92022-04-21 09:52:35 +0200804 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 Krejcie9f13b12020-11-09 17:42:04 +0100812 case 'm': /* --merge */
813 c->data_merge = 1;
814 break;
815
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100816 case 'y': /* --yang-library */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100817 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 Vaskoc431a0a2021-01-25 14:31:58 +0100823 break;
824
stewegd8e2fc92023-05-31 09:52:56 +0200825 case 'X': /* --extended-leafref */
826 c->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
827 break;
828
Radek Krejcied5acc52019-04-25 15:57:04 +0200829#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100830 case 'G': { /* --debug */
831 uint32_t dbg_groups = 0;
832 const char *ptr = optarg;
833
Radek Krejcied5acc52019-04-25 15:57:04 +0200834 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100835 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100836 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100837 ptr += sizeof "dict" - 1;
838 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100839 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100840 ptr += sizeof "xpath" - 1;
Michal Vasko27a4acb2021-11-22 10:03:46 +0100841 } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {
842 dbg_groups |= LY_LDGDEPSETS;
843 ptr += sizeof "dep-sets" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200844 }
845
846 if (ptr[0]) {
847 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100848 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
849 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200850 }
851 ++ptr;
852 }
853 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100854 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200855 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100856 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200857#endif
Michal Vasko27a4acb2021-11-22 10:03:46 +0100858 default:
859 YLMSG_E("Invalid option or missing argument: -%c\n", optopt);
860 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100861 } /* switch */
862 }
863
Radek Krejcie9f13b12020-11-09 17:42:04 +0100864 /* 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
aPiecek1b629762023-04-12 13:52:30 +0200871 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 Krejcie9f13b12020-11-09 17:42:04 +0100883 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 */
aPiecek3d46fa92023-05-10 08:30:39 +0200886 YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100887 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 Krejcied5acc52019-04-25 15:57:04 +0200891 }
892 }
893
Radek Krejcie9f13b12020-11-09 17:42:04 +0100894 if (c->data_operational.path && !c->data_type) {
aPiecekce481582023-04-26 15:44:59 +0200895 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100896 c->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200897 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100898
Michal Vasko3f08fb92022-04-21 09:52:35 +0200899 if (c->reply_rpc.path && (c->data_type != LYD_TYPE_REPLY_NETCONF)) {
aPiecekce481582023-04-26 15:44:59 +0200900 YLMSG_W("Source RPC is needed only for NETCONF Reply input data type.\n");
Michal Vasko3f08fb92022-04-21 09:52:35 +0200901 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
aPiecek2cfeeae2021-04-21 13:17:34 +0200907 if ((c->schema_out_format != LYS_OUT_TREE) && c->line_length) {
aPiecekce481582023-04-26 15:44:59 +0200908 YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n");
aPiecek2cfeeae2021-04-21 13:17:34 +0200909 }
910
aPieceka4723482023-05-11 14:31:02 +0200911 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 Krejcie9f13b12020-11-09 17:42:04 +0100915 /* 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 Krejcied5acc52019-04-25 15:57:04 +0200920 }
921 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100922
aPiecek7ac66d52021-05-20 07:54:07 +0200923 if (c->schema_out_format == LYS_OUT_TREE) {
924 /* print tree from lysc_nodes */
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100925 c->ctx_options |= LY_CTX_SET_PRIV_PARSED;
aPiecek7ac66d52021-05-20 07:54:07 +0200926 }
927
Radek Krejcie9f13b12020-11-09 17:42:04 +0100928 /* 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 Vasko667ce6b2021-01-25 15:00:27 +0100946 if (((c->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || c->data_type) && !c->data_inputs.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100947 YLMSG_W("Data parser options specified, but no data input file provided.\n");
948 }
949
950 if (c->schema_node_path) {
aPiecek7941e322023-05-10 15:44:41 +0200951 /* turn off logging so that the message is not repeated */
952 ly_temp_log_options(&temp_lo);
953 /* search operation input */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100954 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 0);
955 if (!c->schema_node) {
aPiecek7941e322023-05-10 15:44:41 +0200956 /* restore logging so an error may be displayed */
957 ly_temp_log_options(NULL);
958 /* search operation output */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100959 c->schema_node = lys_find_path(c->ctx, NULL, c->schema_node_path, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100960 if (!c->schema_node) {
961 YLMSG_E("Invalid schema path.\n");
962 return -1;
963 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200964 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200965 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100966
Radek Krejcie9f13b12020-11-09 17:42:04 +0100967 return 0;
968}
969
970int
971main_ni(int argc, char *argv[])
972{
973 int ret = EXIT_SUCCESS, r;
974 struct context c = {0};
roman300b8782022-08-11 12:49:21 +0200975 char *features_output = NULL;
976 struct ly_set set = {0};
977 uint32_t u;
Radek Krejcied5acc52019-04-25 15:57:04 +0200978
979 /* set callback for printing libyang messages */
980 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100981
982 r = fill_context(argc, argv, &c);
983 if (r < 0) {
984 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200985 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100986 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200987 goto cleanup;
988 }
989
Radek Krejcie9f13b12020-11-09 17:42:04 +0100990 /* do the required job - parse, validate, print */
991
992 if (c.list) {
993 /* print the list of schemas */
aPiecek34262622023-03-27 08:28:25 +0200994 ret = print_list(c.out, c.ctx, c.data_out_format);
995 goto cleanup;
Radek Krejci047d64e2020-12-03 12:57:10 +0100996 } else {
roman300b8782022-08-11 12:49:21 +0200997 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 Krejci047d64e2020-12-03 12:57:10 +01001011 if (c.schema_node) {
aPiecek146fd192023-03-30 11:22:16 +02001012 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 +01001013 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +01001014 YLMSG_E("Unable to print schema node %s.\n", c.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +01001015 goto cleanup;
1016 }
Michal Vaskofe74d1e2021-09-30 13:17:36 +02001017 } else if (c.submodule) {
1018 const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(c.ctx, c.submodule);
Michal Vasko26bbb272022-08-02 14:54:33 +02001019
Michal Vaskofe74d1e2021-09-30 13:17:36 +02001020 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 Krejci047d64e2020-12-03 12:57:10 +01001030 } else {
roman300b8782022-08-11 12:49:21 +02001031 for (u = 0; u < c.schema_modules.count; ++u) {
aPiecek2cfeeae2021-04-21 13:17:34 +02001032 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);
aPiecek8f0b7882021-05-21 10:18:36 +02001034 /* 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 Krejci047d64e2020-12-03 12:57:10 +01001038 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 Krejcie9f13b12020-11-09 17:42:04 +01001043 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001044 }
Radek Krejci047d64e2020-12-03 12:57:10 +01001045
1046 /* do the data validation despite the schema was printed */
1047 if (c.data_inputs.size) {
Michal Vasko0f1e4562022-03-29 11:57:57 +02001048 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 +02001049 c.data_validate_options, c.data_print_options, &c.data_operational, &c.reply_rpc, &c.data_inputs,
1050 &c.data_xpath);
Michal Vasko0f1e4562022-03-29 11:57:57 +02001051 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +01001052 goto cleanup;
1053 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001054 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001055 }
Radek Krejcied5acc52019-04-25 15:57:04 +02001056
1057cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +01001058 /* cleanup */
1059 erase_context(&c);
roman300b8782022-08-11 12:49:21 +02001060 free(features_output);
1061 ly_set_erase(&set, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +02001062
Radek Krejcied5acc52019-04-25 15:57:04 +02001063 return ret;
1064}