blob: 9bdb8d96eea44253278850b7c9fd54a2a619ed3f [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>
aPieceka83b8e02023-06-07 15:25:16 +02005 * @author Adam Piecek <piecek@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02006 * @brief libyang's yanglint tool - non-interactive code
Radek Krejcied5acc52019-04-25 15:57:04 +02007 *
aPieceka83b8e02023-06-07 15:25:16 +02008 * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
Radek Krejcied5acc52019-04-25 15:57:04 +02009 *
10 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * https://opensource.org/licenses/BSD-3-Clause
15 */
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#define _GNU_SOURCE
Radek Krejcied5acc52019-04-25 15:57:04 +020018
Radek Krejcie9f13b12020-11-09 17:42:04 +010019#include <errno.h>
20#include <getopt.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <stdint.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020022#include <stdio.h>
23#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020024#include <string.h>
Radek Krejci47fab892020-11-05 17:02:41 +010025#include <strings.h>
Radek Krejcie9f13b12020-11-09 17:42:04 +010026#include <sys/stat.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020027
Radek Krejcied5acc52019-04-25 15:57:04 +020028#include "libyang.h"
29
aPiecekbc39de12023-06-08 08:10:29 +020030#include "cmd.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010031#include "common.h"
aPiecek8f0b7882021-05-21 10:18:36 +020032#include "out.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "tools/config.h"
aPieceka83b8e02023-06-07 15:25:16 +020034#include "yl_opt.h"
aPiecekd8f002f2023-06-21 10:40:41 +020035#include "yl_schema_features.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010036
37static void
38version(void)
39{
40 printf("yanglint %s\n", PROJECT_VERSION);
41}
42
43static void
Radek Krejcied5acc52019-04-25 15:57:04 +020044help(int shortout)
45{
Radek Krejcie9f13b12020-11-09 17:42:04 +010046
Michal Vasko3f08fb92022-04-21 09:52:35 +020047 printf("Example usage:\n"
48 " yanglint [-f { yang | yin | info}] <schema>...\n"
49 " Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n"
50 " them in the specified format.\n\n"
51 " yanglint [-f { xml | json }] <schema>... <file>...\n"
52 " Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n"
53 " printing them in the specified format.\n\n"
54 " yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n"
55 " Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n"
aPieceka40764b2023-04-27 15:34:56 +020056 " <operational-file> with possible references to the operational datastore data.\n"
57 " To validate nested-notification or action, the <operational-file> is required.\n\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +020058 " yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n"
59 " Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n"
60 " using <operational-file> with possible references to the operational datastore data.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010061 " yanglint\n"
62 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020063
64 if (shortout) {
65 return;
66 }
Radek Krejcie9f13b12020-11-09 17:42:04 +010067 printf("Options:\n"
68 " -h, --help Show this help message and exit.\n"
69 " -v, --version Show version number and exit.\n"
Michal Vaskob0d307b2021-11-25 11:15:16 +010070 " -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n"
71 " a second time, show even debug messages.\n"
72 " -Q, --quiet Decrease libyang verbosity and hide warnings. If specified a second\n"
73 " time, hide errors so no libyang messages are printed.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010074
Michal Vaskoc40548b2021-09-30 13:05:47 +020075 printf(" -f FORMAT, --format=FORMAT\n"
76 " Convert input into FORMAT. Supported formats: \n"
roman300b8782022-08-11 12:49:21 +020077 " yang, yin, tree, info and feature-param for schemas,\n"
Michal Vaskod8b0e772022-03-18 13:36:04 +010078 " xml, json, and lyb for data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +020079
aPiecekef0a3392023-05-16 10:34:32 +020080 printf(" -I FORMAT, --in-format=FORMAT\n"
81 " Load the data in one of the following formats:\n"
82 " xml, json, lyb\n"
83 " If input format not specified, it is detected from the file extension.\n\n");
84
Michal Vaskoc40548b2021-09-30 13:05:47 +020085 printf(" -p PATH, --path=PATH\n"
86 " Search path for schema (YANG/YIN) modules. The option can be\n"
87 " used multiple times. The current working directory and the\n"
aPiecek4f306da2023-03-27 09:06:07 +020088 " path of the module being added is used implicitly. Subdirectories\n"
89 " are also searched\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010090
Michal Vasko22e0f3a2021-09-22 12:19:23 +020091 printf(" -D, --disable-searchdir\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010092 " Do not implicitly search in current working directory for\n"
93 " schema modules. If specified a second time, do not even\n"
94 " search in the module directory (all modules must be \n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +020095 " explicitly specified).\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010096
Michal Vasko22e0f3a2021-09-22 12:19:23 +020097 printf(" -F FEATURES, --features=FEATURES\n"
Michal Vasko59740872021-11-22 10:01:34 +010098 " Specific module features to support in the form <module-name>:(<feature>,)*\n"
99 " Use <feature> '*' to enable all features of a module. This option can be\n"
100 " specified multiple times, to enable features in multiple modules. If this\n"
101 " option is not specified, all the features in all the implemented modules\n"
102 " are enabled.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100103
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200104 printf(" -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100105 " Make the imported modules \"referenced\" from any loaded\n"
106 " module also implemented. If specified a second time, all the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200107 " modules are set implemented.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100108
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200109 printf(" -P PATH, --schema-node=PATH\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100110 " Print only the specified subtree of the schema.\n"
111 " The PATH is the XPath subset mentioned in documentation as\n"
112 " the Path format. The option can be combined with --single-node\n"
113 " option to print information only about the specified node.\n"
114 " -q, --single-node\n"
115 " Supplement to the --schema-node option to print information\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200116 " only about a single node specified as PATH argument.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100117
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200118 printf(" -s SUBMODULE, --submodule=SUBMODULE\n"
119 " Print the specific submodule instead of the main module.\n\n");
120
ekinzie0ab8b302022-10-10 03:03:57 -0400121 printf(" -x FILE, --ext-data=FILE\n"
122 " File containing the specific data required by an extension. Required by\n"
Michal Vasko14662802022-11-08 08:45:46 +0100123 " the schema-mount extension, for example, when the operational data are\n"
ekinzie0ab8b302022-10-10 03:03:57 -0400124 " expected in the file. File format is guessed.\n\n");
125
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200126 printf(" -n, --not-strict\n"
Michal Vasko667ce6b2021-01-25 15:00:27 +0100127 " Do not require strict data parsing (silently skip unknown data),\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200128 " has no effect for schemas.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100129
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200130 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100131 " exist in the provided input data files. Takes effect only\n"
132 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
133 " mandatory nodes from modules which data are not present in the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200134 " provided input data files.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100135
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200136 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100137 " Specify data tree type in the input data file(s):\n"
138 " data - Complete datastore with status data (default type).\n"
139 " config - Configuration datastore (without status data).\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +0200140 " get - Data returned by the NETCONF <get> operation.\n"
141 " getconfig - Data returned by the NETCONF <get-config> operation.\n"
142 " edit - Config content of the NETCONF <edit-config> operation.\n"
143 " rpc - Invocation of a YANG RPC/action, defined as input.\n"
144 " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n"
145 " envelopes <rpc> or <action>.\n"
146 " reply - Reply to a YANG RPC/action, defined as output. Note that\n"
147 " the reply data are expected inside a container representing\n"
148 " the original RPC/action invocation.\n"
149 " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n"
150 " envelope <rpc-reply> with output data nodes as direct\n"
151 " descendants. The original RPC/action invocation is expected\n"
152 " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n"
153 " notif - Notification instance of a YANG notification.\n"
154 " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n"
155 " envelope <notification> with element <eventTime> and its\n"
156 " sibling as the actual notification.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200157
158 printf(" -d MODE, --default=MODE\n"
159 " Print data with default values, according to the MODE\n"
160 " (to print attributes, ietf-netconf-with-defaults model\n"
161 " must be loaded):\n"
162 " all - Add missing default nodes.\n"
163 " all-tagged - Add missing default nodes and mark all the default\n"
164 " nodes with the attribute.\n"
165 " trim - Remove all nodes with a default value.\n"
166 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n");
167
aPiecek1b629762023-04-12 13:52:30 +0200168 printf(" -E XPATH, --data-xpath=XPATH\n"
aPiecek41955272023-05-10 16:10:17 +0200169 " Evaluate XPATH expression over the data and print the nodes satisfying\n"
aPiecek1b629762023-04-12 13:52:30 +0200170 " the expression. The output format is specific and the option cannot\n"
171 " be combined with the -f and -d options. Also all the data\n"
172 " inputs are merged into a single data tree where the expression\n"
173 " is evaluated, so the -m option is always set implicitly.\n\n");
174
Michal Vaskoc40548b2021-09-30 13:05:47 +0200175 printf(" -l, --list Print info about the loaded schemas.\n"
176 " (i - imported module, I - implemented module)\n"
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100177 " In case the '-f' option with data encoding is specified,\n"
178 " the list is printed as \"ietf-yang-library\" data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200179
180 printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
181 " The limit of the maximum line length on which the 'tree'\n"
182 " format will try to be printed.\n\n");
183
184 printf(" -o OUTFILE, --output=OUTFILE\n"
185 " Write the output to OUTFILE instead of stdout.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100186
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200187 printf(" -O FILE, --operational=FILE\n"
Michal Vasko9e81ce52022-04-29 10:16:21 +0200188 " Provide optional data to extend validation of the '(nc-)rpc',\n"
189 " '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n"
190 " the operational datastore referenced from the operation.\n"
aPieceka40764b2023-04-27 15:34:56 +0200191 " In case of a nested notification or action, its parent existence\n"
192 " is also checked in these operational data.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100193
Michal Vasko3f08fb92022-04-21 09:52:35 +0200194 printf(" -R FILE, --reply-rpc=FILE\n"
195 " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n"
196 " is supposed to contain the source 'nc-rpc' operation of the reply.\n\n");
197
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200198 printf(" -m, --merge Merge input data files into a single tree and validate at\n"
Michal Vasko06158fb2022-03-29 12:13:16 +0200199 " once. The option has effect only for 'data' and 'config' TYPEs.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100200
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200201 printf(" -y, --yang-library\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100202 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
203 " Note that this module includes definitions of mandatory state\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200204 " data that can result in unexpected data validation errors.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200205
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100206 printf(" -Y FILE, --yang-library-file=FILE\n"
207 " Parse FILE with \"ietf-yang-library\" data and use them to\n"
208 " create an exact YANG schema context. If specified, the '-F'\n"
209 " parameter (enabled features) is ignored.\n\n");
210
stewegd8e2fc92023-05-31 09:52:56 +0200211 printf(" -X, --extended-leafref\n"
212 " Allow usage of deref() XPath function within leafref\n\n");
213
stewegb3e420c2024-04-05 08:05:29 +0200214 printf(" -J, --json-null\n"
215 " Allow usage of JSON empty values ('null') within input data\n\n");
216
Michal Vaskoc40548b2021-09-30 13:05:47 +0200217 printf(" -G GROUPS, --debug=GROUPS\n"
Michal Vasko34e2a552024-01-16 15:27:52 +0100218#ifndef NDEBUG
Michal Vaskoc40548b2021-09-30 13:05:47 +0200219 " Enable printing of specific debugging message group\n"
220 " (nothing will be printed unless verbosity is set to debug):\n"
Michal Vasko34e2a552024-01-16 15:27:52 +0100221 " <group>[,<group>]* (dict, xpath, dep-sets)\n\n"
222#else
223 " Unsupported for the Release build\n\n"
Michal Vaskoc40548b2021-09-30 13:05:47 +0200224#endif
Michal Vasko07d1b652024-01-16 15:38:17 +0100225 );
Radek Krejcied5acc52019-04-25 15:57:04 +0200226}
227
Radek Krejcie9f13b12020-11-09 17:42:04 +0100228static void
Michal Vasko7a266772024-01-23 11:02:38 +0100229libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *data_path, const char *schema_path, uint64_t line)
Radek Krejcied5acc52019-04-25 15:57:04 +0200230{
231 char *levstr;
232
Radek Krejcie9f13b12020-11-09 17:42:04 +0100233 switch (level) {
234 case LY_LLERR:
235 levstr = "err :";
236 break;
237 case LY_LLWRN:
238 levstr = "warn:";
239 break;
240 case LY_LLVRB:
241 levstr = "verb:";
242 break;
243 default:
244 levstr = "dbg :";
245 break;
246 }
Michal Vasko7a266772024-01-23 11:02:38 +0100247 if (data_path) {
248 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, data_path);
249 } else if (schema_path) {
250 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, schema_path);
251 } else if (line) {
252 fprintf(stderr, "libyang %s %s (line %" PRIu64 ")\n", levstr, msg, line);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100253 } else {
254 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200255 }
256}
257
aPiecekceae6332023-06-21 10:46:08 +0200258static struct yl_schema_features *
Michal Vasko686d8fc2021-11-22 10:03:23 +0100259get_features_not_applied(const struct ly_set *fset)
260{
261 for (uint32_t u = 0; u < fset->count; ++u) {
aPiecekceae6332023-06-21 10:46:08 +0200262 struct yl_schema_features *sf = fset->objs[u];
Michal Vasko26bbb272022-08-02 14:54:33 +0200263
Michal Vasko686d8fc2021-11-22 10:03:23 +0100264 if (!sf->applied) {
265 return sf;
266 }
267 }
268
269 return NULL;
270}
271
aPiecek4ceaf182023-06-12 14:52:51 +0200272/**
273 * @brief Create the libyang context.
274 *
275 * @param[in] yang_lib_file Context can be defined in yang library file.
276 * @param[in] searchpaths Directories in which modules are searched.
277 * @param[in,out] schema_features Set of features.
278 * @param[in,out] ctx_options Options for libyang context.
279 * @param[out] ctx Context for libyang.
280 * @return 0 on success.
281 */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100282static int
aPiecek4ceaf182023-06-12 14:52:51 +0200283create_ly_context(const char *yang_lib_file, const char *searchpaths, struct ly_set *schema_features,
284 uint16_t *ctx_options, struct ly_ctx **ctx)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100285{
aPiecek4ceaf182023-06-12 14:52:51 +0200286 if (yang_lib_file) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100287 /* ignore features */
aPiecekd8f002f2023-06-21 10:40:41 +0200288 ly_set_erase(schema_features, yl_schema_features_free);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100289
aPiecek4ceaf182023-06-12 14:52:51 +0200290 if (ly_ctx_new_ylpath(searchpaths, yang_lib_file, LYD_UNKNOWN, *ctx_options, ctx)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200291 YLMSG_E("Unable to modify libyang context with yang-library data.");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100292 return -1;
293 }
294 } else {
295 /* set imp feature flag if all should be enabled */
aPiecek4ceaf182023-06-12 14:52:51 +0200296 (*ctx_options) |= !schema_features->count ? LY_CTX_ENABLE_IMP_FEATURES : 0;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100297
aPiecek4ceaf182023-06-12 14:52:51 +0200298 if (ly_ctx_new(searchpaths, *ctx_options, ctx)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200299 YLMSG_E("Unable to create libyang context.");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100300 return -1;
301 }
aPiecek912f3d52022-10-12 10:02:33 +0200302 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100303
aPiecek4ceaf182023-06-12 14:52:51 +0200304 return 0;
305}
306
307/**
308 * @brief Implement module if some feature has not been applied.
309 *
310 * @param[in] schema_features Set of features.
311 * @param[in,out] ctx Context for libyang.
312 * @return 0 on success.
313 */
314static int
315apply_features(struct ly_set *schema_features, struct ly_ctx *ctx)
316{
aPiecekceae6332023-06-21 10:46:08 +0200317 struct yl_schema_features *sf;
aPiecek4ceaf182023-06-12 14:52:51 +0200318 struct lys_module *mod;
319
320 /* check that all specified features were applied, apply now if possible */
321 while ((sf = get_features_not_applied(schema_features))) {
322 /* try to find implemented or the latest revision of this module */
323 mod = ly_ctx_get_module_implemented(ctx, sf->mod_name);
324 if (!mod) {
325 mod = ly_ctx_get_module_latest(ctx, sf->mod_name);
326 }
327 if (!mod) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200328 YLMSG_E("Specified features not applied, module \"%s\" not loaded.", sf->mod_name);
aPiecek4ceaf182023-06-12 14:52:51 +0200329 return 1;
330 }
331
332 /* we have the module, implement it if needed and enable the specific features */
333 if (lys_set_implemented(mod, (const char **)sf->features)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200334 YLMSG_E("Implementing module \"%s\" failed.", mod->name);
aPiecek4ceaf182023-06-12 14:52:51 +0200335 return 1;
336 }
337 sf->applied = 1;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100338 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100339
aPiecek4ceaf182023-06-12 14:52:51 +0200340 return 0;
341}
342
343/**
344 * @brief Parse and compile modules, data are only stored for later processing.
345 *
346 * @param[in] argc Number of strings in @p argv.
347 * @param[in] argv Strings from command line.
348 * @param[in] optind Index to the first input file in @p argv.
349 * @param[in] data_in_format Specified input data format.
350 * @param[in,out] ctx Context for libyang.
351 * @param[in,out] yo Options for yanglint.
aPiecek4ceaf182023-06-12 14:52:51 +0200352 * @return 0 on success.
353 */
354static int
355fill_context_inputs(int argc, char *argv[], int optind, LYD_FORMAT data_in_format, struct ly_ctx *ctx,
aPiecek618d1cc2023-06-15 08:31:27 +0200356 struct yl_opt *yo)
aPiecek4ceaf182023-06-12 14:52:51 +0200357{
aPiecek4ceaf182023-06-12 14:52:51 +0200358 char *filepath = NULL;
359 LYS_INFORMAT format_schema;
360 LYD_FORMAT format_data;
361
Radek Krejcie9f13b12020-11-09 17:42:04 +0100362 for (int i = 0; i < argc - optind; i++) {
aPiecek4ceaf182023-06-12 14:52:51 +0200363 format_schema = LYS_IN_UNKNOWN;
364 format_data = data_in_format;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100365
aPieceka83b8e02023-06-07 15:25:16 +0200366 filepath = argv[optind + i];
367
368 if (!filepath) {
aPiecek618d1cc2023-06-15 08:31:27 +0200369 return -1;
aPieceka83b8e02023-06-07 15:25:16 +0200370 }
aPiecek618d1cc2023-06-15 08:31:27 +0200371 if (get_format(filepath, &format_schema, &format_data)) {
372 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100373 }
374
375 if (format_schema) {
aPiecek4ceaf182023-06-12 14:52:51 +0200376 if (cmd_add_exec(&ctx, yo, filepath)) {
aPiecek618d1cc2023-06-15 08:31:27 +0200377 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100378 }
aPiecek2457b5e2023-06-12 11:40:14 +0200379 } else {
aPiecekcf9e7682023-06-19 14:03:50 +0200380 if (cmd_data_store(&ctx, yo, filepath)) {
aPiecek618d1cc2023-06-15 08:31:27 +0200381 return -1;
aPieceka6d45b52023-05-19 11:48:59 +0200382 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100383 }
384 }
385
aPiecek4ceaf182023-06-12 14:52:51 +0200386 /* Check that all specified features were applied, apply now if possible. */
387 if (apply_features(&yo->schema_features, ctx)) {
388 return -1;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100389 }
390
Radek Krejcie9f13b12020-11-09 17:42:04 +0100391 return 0;
392}
393
aPiecek0cbc9022023-06-08 16:58:56 +0200394/**
395 * @brief Enable specific debugging messages.
396 *
aPiecek27337da2023-06-19 14:49:29 +0200397 * @param[in] groups String in the form "<group>[,group>]*".
aPiecek0cbc9022023-06-08 16:58:56 +0200398 * @param[in,out] yo Options for yanglint.
399 * return 0 on success.
400 */
401static int
402set_debug_groups(char *groups, struct yl_opt *yo)
403{
404 int rc;
405 char *str, *end;
406
407 /* Process all debug arguments except the last one. */
408 for (str = groups; (end = strchr(str, ',')); str = end + 1) {
409 /* Temporary modify input string. */
410 *end = '\0';
aPiecekcf9e7682023-06-19 14:03:50 +0200411 rc = cmd_debug_store(NULL, yo, str);
aPiecek0cbc9022023-06-08 16:58:56 +0200412 *end = ',';
413 if (rc) {
414 return -1;
415 }
416 }
417 /* Process single/last debug argument. */
aPiecekcf9e7682023-06-19 14:03:50 +0200418 if (cmd_debug_store(NULL, yo, str)) {
aPiecek0cbc9022023-06-08 16:58:56 +0200419 return -1;
420 }
421 /* All debug arguments are valid, so they can apply. */
aPiecekcf9e7682023-06-19 14:03:50 +0200422 if (cmd_debug_setlog(NULL, yo)) {
aPiecek0cbc9022023-06-08 16:58:56 +0200423 return -1;
424 }
425
426 return 0;
427}
428
Radek Krejcie9f13b12020-11-09 17:42:04 +0100429/**
430 * @brief Process command line options and store the settings into the context.
431 *
432 * return -1 in case of error;
433 * return 0 in case of success and ready to process
434 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200435 */
436static int
aPieceka83b8e02023-06-07 15:25:16 +0200437fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
Radek Krejcied5acc52019-04-25 15:57:04 +0200438{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100439 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200440 struct option options[] = {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100441 {"help", no_argument, NULL, 'h'},
442 {"version", no_argument, NULL, 'v'},
443 {"verbose", no_argument, NULL, 'V'},
444 {"quiet", no_argument, NULL, 'Q'},
445 {"format", required_argument, NULL, 'f'},
446 {"path", required_argument, NULL, 'p'},
Michal Vaskod8b0e772022-03-18 13:36:04 +0100447 {"disable-searchdir", no_argument, NULL, 'D'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100448 {"features", required_argument, NULL, 'F'},
449 {"make-implemented", no_argument, NULL, 'i'},
aPiecekef0a3392023-05-16 10:34:32 +0200450 {"in-format", required_argument, NULL, 'I'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100451 {"schema-node", required_argument, NULL, 'P'},
452 {"single-node", no_argument, NULL, 'q'},
453 {"submodule", required_argument, NULL, 's'},
ekinzie0ab8b302022-10-10 03:03:57 -0400454 {"ext-data", required_argument, NULL, 'x'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100455 {"not-strict", no_argument, NULL, 'n'},
456 {"present", no_argument, NULL, 'e'},
457 {"type", required_argument, NULL, 't'},
458 {"default", required_argument, NULL, 'd'},
aPiecek1b629762023-04-12 13:52:30 +0200459 {"data-xpath", required_argument, NULL, 'E'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100460 {"list", no_argument, NULL, 'l'},
461 {"tree-line-length", required_argument, NULL, 'L'},
462 {"output", required_argument, NULL, 'o'},
463 {"operational", required_argument, NULL, 'O'},
Michal Vasko3f08fb92022-04-21 09:52:35 +0200464 {"reply-rpc", required_argument, NULL, 'R'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100465 {"merge", no_argument, NULL, 'm'},
466 {"yang-library", no_argument, NULL, 'y'},
467 {"yang-library-file", required_argument, NULL, 'Y'},
stewegd8e2fc92023-05-31 09:52:56 +0200468 {"extended-leafref", no_argument, NULL, 'X'},
stewegb3e420c2024-04-05 08:05:29 +0200469 {"json-null", no_argument, NULL, 'J'},
Michal Vasko34e2a552024-01-16 15:27:52 +0100470 {"debug", required_argument, NULL, 'G'},
Radek Krejcied5acc52019-04-25 15:57:04 +0200471 {NULL, 0, NULL, 0}
472 };
Radek Krejcie9f13b12020-11-09 17:42:04 +0100473 uint8_t data_type_set = 0;
474
aPieceka83b8e02023-06-07 15:25:16 +0200475 yo->ctx_options = YL_DEFAULT_CTX_OPTIONS;
476 yo->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
477 yo->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
478 yo->line_length = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100479
Michal Vasko27a4acb2021-11-22 10:03:46 +0100480 opterr = 0;
stewegb3e420c2024-04-05 08:05:29 +0200481 while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:XJx:G:", options, &opt_index)) != -1) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200482 switch (opt) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200483 case 'h': /* --help */
484 help(0);
485 return 1;
486
487 case 'v': /* --version */
488 version();
489 return 1;
490
491 case 'V': { /* --verbose */
492 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100493
Michal Vaskoc40548b2021-09-30 13:05:47 +0200494 if (verbosity < LY_LLDBG) {
Michal Vaskob0d307b2021-11-25 11:15:16 +0100495 ++verbosity;
Radek Krejcied5acc52019-04-25 15:57:04 +0200496 }
Michal Vaskob0d307b2021-11-25 11:15:16 +0100497 ly_log_level(verbosity);
Radek Krejcied5acc52019-04-25 15:57:04 +0200498 break;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200499 } /* case 'V' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100500
Michal Vaskob0d307b2021-11-25 11:15:16 +0100501 case 'Q': { /* --quiet */
502 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100503
Michal Vaskob0d307b2021-11-25 11:15:16 +0100504 if (verbosity == LY_LLERR) {
505 /* turn logging off */
506 ly_log_options(LY_LOSTORE_LAST);
507 } else if (verbosity > LY_LLERR) {
508 --verbosity;
509 }
510 ly_log_level(verbosity);
511 break;
512 } /* case 'Q' */
513
Radek Krejcie9f13b12020-11-09 17:42:04 +0100514 case 'f': /* --format */
aPiecek113e0f02023-06-09 08:47:48 +0200515 if (yl_opt_update_out_format(optarg, yo)) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200516 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100517 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200518 }
519 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100520
aPiecekef0a3392023-05-16 10:34:32 +0200521 case 'I': /* --in-format */
aPiecekb5dff492023-06-09 09:38:08 +0200522 if (yo_opt_update_data_in_format(optarg, yo)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200523 YLMSG_E("Unknown input format %s.", optarg);
aPiecekef0a3392023-05-16 10:34:32 +0200524 help(1);
525 return -1;
526 }
527 break;
528
aPiecek113e0f02023-06-09 08:47:48 +0200529 case 'p': /* --path */
aPieceka83b8e02023-06-07 15:25:16 +0200530 if (searchpath_strcat(&yo->searchpaths, optarg)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200531 YLMSG_E("Storing searchpath failed.");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200532 return -1;
533 }
Michal Vaskoc40548b2021-09-30 13:05:47 +0200534 break;
aPiecek113e0f02023-06-09 08:47:48 +0200535 /* case 'p' */
Michal Vaskoc40548b2021-09-30 13:05:47 +0200536
aPiecek88ae15e2023-06-09 10:20:17 +0200537 case 'D': /* --disable-searchdir */
aPieceka83b8e02023-06-07 15:25:16 +0200538 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200539 YLMSG_W("The -D option specified too many times.");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200540 }
aPiecek88ae15e2023-06-09 10:20:17 +0200541 yo_opt_update_disable_searchdir(yo);
Michal Vaskoc40548b2021-09-30 13:05:47 +0200542 break;
543
544 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +0200545 if (parse_features(optarg, &yo->schema_features)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200546 return -1;
547 }
548 break;
549
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200550 case 'i': /* --make-implemented */
aPiecek7f22c102023-06-09 09:48:44 +0200551 yo_opt_update_make_implemented(yo);
Michal Vaskoc40548b2021-09-30 13:05:47 +0200552 break;
553
Radek Krejcie9f13b12020-11-09 17:42:04 +0100554 case 'P': /* --schema-node */
aPieceka83b8e02023-06-07 15:25:16 +0200555 yo->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200556 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100557
558 case 'q': /* --single-node */
aPieceka83b8e02023-06-07 15:25:16 +0200559 yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100560 break;
561
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200562 case 's': /* --submodule */
aPieceka83b8e02023-06-07 15:25:16 +0200563 yo->submodule = optarg;
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200564 break;
565
ekinzie0ab8b302022-10-10 03:03:57 -0400566 case 'x': /* --ext-data */
aPieceka83b8e02023-06-07 15:25:16 +0200567 yo->schema_context_filename = optarg;
ekinzie0ab8b302022-10-10 03:03:57 -0400568 break;
569
Michal Vasko667ce6b2021-01-25 15:00:27 +0100570 case 'n': /* --not-strict */
aPieceka83b8e02023-06-07 15:25:16 +0200571 yo->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100572 break;
573
574 case 'e': /* --present */
aPieceka83b8e02023-06-07 15:25:16 +0200575 yo->data_validate_options |= LYD_VALIDATE_PRESENT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100576 break;
577
578 case 't': /* --type */
579 if (data_type_set) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200580 YLMSG_E("The data type (-t) cannot be set multiple times.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100581 return -1;
582 }
583
aPiecek3167f382023-06-09 09:23:10 +0200584 if (yl_opt_update_data_type(optarg, yo)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200585 YLMSG_E("Unknown data tree type %s.", optarg);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100586 help(1);
587 return -1;
588 }
589
590 data_type_set = 1;
591 break;
592
Michal Vaskoc40548b2021-09-30 13:05:47 +0200593 case 'd': /* --default */
aPiecek20cc2fe2023-06-09 09:32:28 +0200594 if (yo_opt_update_data_default(optarg, yo)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200595 YLMSG_E("Unknown default mode %s.", optarg);
Michal Vaskoc40548b2021-09-30 13:05:47 +0200596 help(1);
597 return -1;
598 }
599 break;
600
aPiecek1b629762023-04-12 13:52:30 +0200601 case 'E': /* --data-xpath */
aPieceka83b8e02023-06-07 15:25:16 +0200602 if (ly_set_add(&yo->data_xpath, optarg, 0, NULL)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200603 YLMSG_E("Storing XPath \"%s\" failed.", optarg);
aPiecek1b629762023-04-12 13:52:30 +0200604 return -1;
605 }
606 break;
607
Michal Vaskoc40548b2021-09-30 13:05:47 +0200608 case 'l': /* --list */
aPieceka83b8e02023-06-07 15:25:16 +0200609 yo->list = 1;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200610 break;
611
612 case 'L': /* --tree-line-length */
aPieceka83b8e02023-06-07 15:25:16 +0200613 yo->line_length = atoi(optarg);
Michal Vaskoc40548b2021-09-30 13:05:47 +0200614 break;
615
616 case 'o': /* --output */
aPieceka83b8e02023-06-07 15:25:16 +0200617 if (yo->out) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200618 YLMSG_E("Only a single output can be specified.");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200619 return -1;
620 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200621 if (ly_out_new_filepath(optarg, &yo->out)) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200622 YLMSG_E("Unable open output file %s (%s).", optarg, strerror(errno));
Michal Vaskoc40548b2021-09-30 13:05:47 +0200623 return -1;
624 }
625 }
626 break;
627
Radek Krejcie9f13b12020-11-09 17:42:04 +0100628 case 'O': /* --operational */
aPieceka83b8e02023-06-07 15:25:16 +0200629 if (yo->data_operational.path) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200630 YLMSG_E("The operational datastore (-O) cannot be set multiple times.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100631 return -1;
632 }
aPieceka83b8e02023-06-07 15:25:16 +0200633 yo->data_operational.path = optarg;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100634 break;
635
Michal Vasko3f08fb92022-04-21 09:52:35 +0200636 case 'R': /* --reply-rpc */
aPieceka83b8e02023-06-07 15:25:16 +0200637 if (yo->reply_rpc.path) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200638 YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.");
Michal Vasko3f08fb92022-04-21 09:52:35 +0200639 return -1;
640 }
aPieceka83b8e02023-06-07 15:25:16 +0200641 yo->reply_rpc.path = optarg;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200642 break;
643
Radek Krejcie9f13b12020-11-09 17:42:04 +0100644 case 'm': /* --merge */
aPieceka83b8e02023-06-07 15:25:16 +0200645 yo->data_merge = 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100646 break;
647
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100648 case 'y': /* --yang-library */
aPieceka83b8e02023-06-07 15:25:16 +0200649 yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100650 break;
651
652 case 'Y': /* --yang-library-file */
aPieceka83b8e02023-06-07 15:25:16 +0200653 yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
654 yo->yang_lib_file = optarg;
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100655 break;
656
stewegd8e2fc92023-05-31 09:52:56 +0200657 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +0200658 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
stewegd8e2fc92023-05-31 09:52:56 +0200659 break;
660
stewegb3e420c2024-04-05 08:05:29 +0200661 case 'J': /* --json-null */
662 yo->data_parse_options |= LYD_PARSE_JSON_NULL;
663 break;
664
aPiecek0cbc9022023-06-08 16:58:56 +0200665 case 'G': /* --debug */
666 if (set_debug_groups(optarg, yo)) {
667 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200668 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200669 break;
Michal Vasko34e2a552024-01-16 15:27:52 +0100670
Michal Vasko27a4acb2021-11-22 10:03:46 +0100671 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +0200672 YLMSG_E("Invalid option or missing argument: -%c.", optopt);
Michal Vasko27a4acb2021-11-22 10:03:46 +0100673 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100674 } /* switch */
675 }
676
Radek Krejcie9f13b12020-11-09 17:42:04 +0100677 /* additional checks for the options combinations */
aPieceka83b8e02023-06-07 15:25:16 +0200678 if (!yo->list && (optind >= argc)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100679 help(1);
Michal Vasko1407d7d2023-08-21 09:57:54 +0200680 YLMSG_E("Missing <schema> to process.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100681 return 1;
682 }
683
aPiecekbc39de12023-06-08 08:10:29 +0200684 if (cmd_data_dep(yo, 0)) {
Michal Vasko3f08fb92022-04-21 09:52:35 +0200685 return -1;
686 }
aPiecek04c8c2e2023-06-08 08:38:22 +0200687 if (cmd_print_dep(yo, 0)) {
688 return -1;
aPiecek2cfeeae2021-04-21 13:17:34 +0200689 }
690
aPiecek4ceaf182023-06-12 14:52:51 +0200691 /* Create the libyang context. */
692 if (create_ly_context(yo->yang_lib_file, yo->searchpaths, &yo->schema_features, &yo->ctx_options, ctx)) {
693 return -1;
694 }
695
696 /* Set callback providing run-time extension instance data. */
697 if (yo->schema_context_filename) {
698 ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, yo->schema_context_filename);
699 }
700
701 /* Schema modules and data files are just checked and prepared into internal structures for further processing. */
aPiecek618d1cc2023-06-15 08:31:27 +0200702 if (fill_context_inputs(argc, argv, optind, yo->data_in_format, *ctx, yo)) {
aPiecek4ceaf182023-06-12 14:52:51 +0200703 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100704 }
705
706 /* the second batch of checks */
aPieceka83b8e02023-06-07 15:25:16 +0200707 if (yo->schema_print_options && !yo->schema_out_format) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200708 YLMSG_W("Schema printer options specified, but the schema output format is missing.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100709 }
aPieceka83b8e02023-06-07 15:25:16 +0200710 if (yo->schema_parse_options && !yo->schema_modules.count) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200711 YLMSG_W("Schema parser options specified, but no schema input file provided.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100712 }
aPieceka83b8e02023-06-07 15:25:16 +0200713 if (yo->data_print_options && !yo->data_out_format) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200714 YLMSG_W("data printer options specified, but the data output format is missing.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100715 }
aPieceka83b8e02023-06-07 15:25:16 +0200716 if (((yo->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || yo->data_type) && !yo->data_inputs.count) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200717 YLMSG_W("Data parser options specified, but no data input file provided.");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100718 }
719
Radek Krejcie9f13b12020-11-09 17:42:04 +0100720 return 0;
721}
722
723int
724main_ni(int argc, char *argv[])
725{
726 int ret = EXIT_SUCCESS, r;
aPieceka83b8e02023-06-07 15:25:16 +0200727 struct yl_opt yo = {0};
728 struct ly_ctx *ctx = NULL;
roman300b8782022-08-11 12:49:21 +0200729 char *features_output = NULL;
730 struct ly_set set = {0};
731 uint32_t u;
Radek Krejcied5acc52019-04-25 15:57:04 +0200732
733 /* set callback for printing libyang messages */
Michal Vasko7a266772024-01-23 11:02:38 +0100734 ly_set_log_clb(libyang_verbclb);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100735
aPieceka83b8e02023-06-07 15:25:16 +0200736 r = fill_context(argc, argv, &yo, &ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100737 if (r < 0) {
738 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200739 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100740 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200741 goto cleanup;
742 }
743
Radek Krejcie9f13b12020-11-09 17:42:04 +0100744 /* do the required job - parse, validate, print */
745
aPieceka83b8e02023-06-07 15:25:16 +0200746 if (yo.list) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100747 /* print the list of schemas */
aPiecek8ec2e832023-06-12 15:35:34 +0200748 ret = cmd_list_exec(&ctx, &yo, NULL);
aPiecek34262622023-03-27 08:28:25 +0200749 goto cleanup;
aPiecek816107f2023-06-15 15:31:53 +0200750 }
751 if (yo.feature_param_format) {
752 for (u = 0; u < yo.schema_modules.count; u++) {
753 if ((ret = cmd_feature_exec(&ctx, &yo, ((struct lys_module *)yo.schema_modules.objs[u])->name))) {
aPiecekf8ad2a02023-06-15 09:10:39 +0200754 goto cleanup;
755 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200756 }
aPiecek6fde6d02023-06-21 15:06:45 +0200757 cmd_feature_fin(ctx, &yo);
aPiecek816107f2023-06-15 15:31:53 +0200758 } else if (yo.schema_out_format && yo.schema_node_path) {
759 if ((ret = cmd_print_exec(&ctx, &yo, NULL))) {
760 goto cleanup;
761 }
762 } else if (yo.schema_out_format && yo.submodule) {
763 if ((ret = cmd_print_exec(&ctx, &yo, yo.submodule))) {
764 goto cleanup;
765 }
766 } else if (yo.schema_out_format) {
767 for (u = 0; u < yo.schema_modules.count; ++u) {
768 yo.last_one = (u + 1) == yo.schema_modules.count;
769 if ((ret = cmd_print_exec(&ctx, &yo, ((struct lys_module *)yo.schema_modules.objs[u])->name))) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100770 goto cleanup;
771 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200772 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200773 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200774
aPiecek816107f2023-06-15 15:31:53 +0200775 /* do the data validation despite the schema was printed */
776 if (yo.data_inputs.size) {
aPiecekcf9e7682023-06-19 14:03:50 +0200777 if ((ret = cmd_data_process(ctx, &yo))) {
aPiecek816107f2023-06-15 15:31:53 +0200778 goto cleanup;
779 }
780 }
781
Radek Krejcied5acc52019-04-25 15:57:04 +0200782cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100783 /* cleanup */
aPieceka83b8e02023-06-07 15:25:16 +0200784 yl_opt_erase(&yo);
785 ly_ctx_destroy(ctx);
roman300b8782022-08-11 12:49:21 +0200786 free(features_output);
787 ly_set_erase(&set, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200788
Radek Krejcied5acc52019-04-25 15:57:04 +0200789 return ret;
790}