blob: 86696a61f27e1cd4c1e731fef01c1d0a59913b35 [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"
Radek Krejcie9f13b12020-11-09 17:42:04 +010035
36static void
37version(void)
38{
39 printf("yanglint %s\n", PROJECT_VERSION);
40}
41
42static void
Radek Krejcied5acc52019-04-25 15:57:04 +020043help(int shortout)
44{
Radek Krejcie9f13b12020-11-09 17:42:04 +010045
Michal Vasko3f08fb92022-04-21 09:52:35 +020046 printf("Example usage:\n"
47 " yanglint [-f { yang | yin | info}] <schema>...\n"
48 " Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n"
49 " them in the specified format.\n\n"
50 " yanglint [-f { xml | json }] <schema>... <file>...\n"
51 " Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n"
52 " printing them in the specified format.\n\n"
53 " yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n"
54 " Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n"
aPieceka40764b2023-04-27 15:34:56 +020055 " <operational-file> with possible references to the operational datastore data.\n"
56 " To validate nested-notification or action, the <operational-file> is required.\n\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +020057 " yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n"
58 " Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n"
59 " using <operational-file> with possible references to the operational datastore data.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010060 " yanglint\n"
61 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020062
63 if (shortout) {
64 return;
65 }
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 printf("Options:\n"
67 " -h, --help Show this help message and exit.\n"
68 " -v, --version Show version number and exit.\n"
Michal Vaskob0d307b2021-11-25 11:15:16 +010069 " -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n"
70 " a second time, show even debug messages.\n"
71 " -Q, --quiet Decrease libyang verbosity and hide warnings. If specified a second\n"
72 " time, hide errors so no libyang messages are printed.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010073
Michal Vaskoc40548b2021-09-30 13:05:47 +020074 printf(" -f FORMAT, --format=FORMAT\n"
75 " Convert input into FORMAT. Supported formats: \n"
roman300b8782022-08-11 12:49:21 +020076 " yang, yin, tree, info and feature-param for schemas,\n"
Michal Vaskod8b0e772022-03-18 13:36:04 +010077 " xml, json, and lyb for data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +020078
aPiecekef0a3392023-05-16 10:34:32 +020079 printf(" -I FORMAT, --in-format=FORMAT\n"
80 " Load the data in one of the following formats:\n"
81 " xml, json, lyb\n"
82 " If input format not specified, it is detected from the file extension.\n\n");
83
Michal Vaskoc40548b2021-09-30 13:05:47 +020084 printf(" -p PATH, --path=PATH\n"
85 " Search path for schema (YANG/YIN) modules. The option can be\n"
86 " used multiple times. The current working directory and the\n"
aPiecek4f306da2023-03-27 09:06:07 +020087 " path of the module being added is used implicitly. Subdirectories\n"
88 " are also searched\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010089
Michal Vasko22e0f3a2021-09-22 12:19:23 +020090 printf(" -D, --disable-searchdir\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010091 " Do not implicitly search in current working directory for\n"
92 " schema modules. If specified a second time, do not even\n"
93 " search in the module directory (all modules must be \n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +020094 " explicitly specified).\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010095
Michal Vasko22e0f3a2021-09-22 12:19:23 +020096 printf(" -F FEATURES, --features=FEATURES\n"
Michal Vasko59740872021-11-22 10:01:34 +010097 " Specific module features to support in the form <module-name>:(<feature>,)*\n"
98 " Use <feature> '*' to enable all features of a module. This option can be\n"
99 " specified multiple times, to enable features in multiple modules. If this\n"
100 " option is not specified, all the features in all the implemented modules\n"
101 " are enabled.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100102
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200103 printf(" -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100104 " Make the imported modules \"referenced\" from any loaded\n"
105 " module also implemented. If specified a second time, all the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200106 " modules are set implemented.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100107
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200108 printf(" -P PATH, --schema-node=PATH\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100109 " Print only the specified subtree of the schema.\n"
110 " The PATH is the XPath subset mentioned in documentation as\n"
111 " the Path format. The option can be combined with --single-node\n"
112 " option to print information only about the specified node.\n"
113 " -q, --single-node\n"
114 " Supplement to the --schema-node option to print information\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200115 " only about a single node specified as PATH argument.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100116
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200117 printf(" -s SUBMODULE, --submodule=SUBMODULE\n"
118 " Print the specific submodule instead of the main module.\n\n");
119
ekinzie0ab8b302022-10-10 03:03:57 -0400120 printf(" -x FILE, --ext-data=FILE\n"
121 " File containing the specific data required by an extension. Required by\n"
Michal Vasko14662802022-11-08 08:45:46 +0100122 " the schema-mount extension, for example, when the operational data are\n"
ekinzie0ab8b302022-10-10 03:03:57 -0400123 " expected in the file. File format is guessed.\n\n");
124
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200125 printf(" -n, --not-strict\n"
Michal Vasko667ce6b2021-01-25 15:00:27 +0100126 " Do not require strict data parsing (silently skip unknown data),\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200127 " has no effect for schemas.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100128
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200129 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100130 " exist in the provided input data files. Takes effect only\n"
131 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
132 " mandatory nodes from modules which data are not present in the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200133 " provided input data files.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100134
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200135 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100136 " Specify data tree type in the input data file(s):\n"
137 " data - Complete datastore with status data (default type).\n"
138 " config - Configuration datastore (without status data).\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +0200139 " get - Data returned by the NETCONF <get> operation.\n"
140 " getconfig - Data returned by the NETCONF <get-config> operation.\n"
141 " edit - Config content of the NETCONF <edit-config> operation.\n"
142 " rpc - Invocation of a YANG RPC/action, defined as input.\n"
143 " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n"
144 " envelopes <rpc> or <action>.\n"
145 " reply - Reply to a YANG RPC/action, defined as output. Note that\n"
146 " the reply data are expected inside a container representing\n"
147 " the original RPC/action invocation.\n"
148 " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n"
149 " envelope <rpc-reply> with output data nodes as direct\n"
150 " descendants. The original RPC/action invocation is expected\n"
151 " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n"
152 " notif - Notification instance of a YANG notification.\n"
153 " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n"
154 " envelope <notification> with element <eventTime> and its\n"
155 " sibling as the actual notification.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200156
157 printf(" -d MODE, --default=MODE\n"
158 " Print data with default values, according to the MODE\n"
159 " (to print attributes, ietf-netconf-with-defaults model\n"
160 " must be loaded):\n"
161 " all - Add missing default nodes.\n"
162 " all-tagged - Add missing default nodes and mark all the default\n"
163 " nodes with the attribute.\n"
164 " trim - Remove all nodes with a default value.\n"
165 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n");
166
aPiecek1b629762023-04-12 13:52:30 +0200167 printf(" -E XPATH, --data-xpath=XPATH\n"
aPiecek41955272023-05-10 16:10:17 +0200168 " Evaluate XPATH expression over the data and print the nodes satisfying\n"
aPiecek1b629762023-04-12 13:52:30 +0200169 " the expression. The output format is specific and the option cannot\n"
170 " be combined with the -f and -d options. Also all the data\n"
171 " inputs are merged into a single data tree where the expression\n"
172 " is evaluated, so the -m option is always set implicitly.\n\n");
173
Michal Vaskoc40548b2021-09-30 13:05:47 +0200174 printf(" -l, --list Print info about the loaded schemas.\n"
175 " (i - imported module, I - implemented module)\n"
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100176 " In case the '-f' option with data encoding is specified,\n"
177 " the list is printed as \"ietf-yang-library\" data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200178
179 printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
180 " The limit of the maximum line length on which the 'tree'\n"
181 " format will try to be printed.\n\n");
182
183 printf(" -o OUTFILE, --output=OUTFILE\n"
184 " Write the output to OUTFILE instead of stdout.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100185
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200186 printf(" -O FILE, --operational=FILE\n"
Michal Vasko9e81ce52022-04-29 10:16:21 +0200187 " Provide optional data to extend validation of the '(nc-)rpc',\n"
188 " '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n"
189 " the operational datastore referenced from the operation.\n"
aPieceka40764b2023-04-27 15:34:56 +0200190 " In case of a nested notification or action, its parent existence\n"
191 " is also checked in these operational data.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100192
Michal Vasko3f08fb92022-04-21 09:52:35 +0200193 printf(" -R FILE, --reply-rpc=FILE\n"
194 " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n"
195 " is supposed to contain the source 'nc-rpc' operation of the reply.\n\n");
196
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200197 printf(" -m, --merge Merge input data files into a single tree and validate at\n"
Michal Vasko06158fb2022-03-29 12:13:16 +0200198 " once. The option has effect only for 'data' and 'config' TYPEs.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100199
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200200 printf(" -y, --yang-library\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100201 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
202 " Note that this module includes definitions of mandatory state\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200203 " data that can result in unexpected data validation errors.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200204
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100205 printf(" -Y FILE, --yang-library-file=FILE\n"
206 " Parse FILE with \"ietf-yang-library\" data and use them to\n"
207 " create an exact YANG schema context. If specified, the '-F'\n"
208 " parameter (enabled features) is ignored.\n\n");
209
stewegd8e2fc92023-05-31 09:52:56 +0200210 printf(" -X, --extended-leafref\n"
211 " Allow usage of deref() XPath function within leafref\n\n");
212
Michal Vaskoc40548b2021-09-30 13:05:47 +0200213#ifndef NDEBUG
214 printf(" -G GROUPS, --debug=GROUPS\n"
215 " Enable printing of specific debugging message group\n"
216 " (nothing will be printed unless verbosity is set to debug):\n"
Michal Vasko27a4acb2021-11-22 10:03:46 +0100217 " <group>[,<group>]* (dict, xpath, dep-sets)\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200218#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200219}
220
Radek Krejcie9f13b12020-11-09 17:42:04 +0100221static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200222libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
223{
224 char *levstr;
225
Radek Krejcie9f13b12020-11-09 17:42:04 +0100226 switch (level) {
227 case LY_LLERR:
228 levstr = "err :";
229 break;
230 case LY_LLWRN:
231 levstr = "warn:";
232 break;
233 case LY_LLVRB:
234 levstr = "verb:";
235 break;
236 default:
237 levstr = "dbg :";
238 break;
239 }
240 if (path) {
241 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
242 } else {
243 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200244 }
245}
246
Michal Vasko686d8fc2021-11-22 10:03:23 +0100247static struct schema_features *
248get_features_not_applied(const struct ly_set *fset)
249{
250 for (uint32_t u = 0; u < fset->count; ++u) {
251 struct schema_features *sf = fset->objs[u];
Michal Vasko26bbb272022-08-02 14:54:33 +0200252
Michal Vasko686d8fc2021-11-22 10:03:23 +0100253 if (!sf->applied) {
254 return sf;
255 }
256 }
257
258 return NULL;
259}
260
Radek Krejcie9f13b12020-11-09 17:42:04 +0100261static int
aPieceka83b8e02023-06-07 15:25:16 +0200262fill_context_inputs(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100263{
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100264 struct ly_in *in = NULL;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100265 struct schema_features *sf;
Michal Vaskoc91fb122021-11-23 09:00:17 +0100266 struct lys_module *mod;
aPiecek912f3d52022-10-12 10:02:33 +0200267 const char *all_features[] = {"*", NULL};
aPieceka83b8e02023-06-07 15:25:16 +0200268 char *dir = NULL, *module = NULL, *filepath = NULL;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100269
aPiecek912f3d52022-10-12 10:02:33 +0200270 /* Create libyang context. */
aPieceka83b8e02023-06-07 15:25:16 +0200271 if (yo->yang_lib_file) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100272 /* ignore features */
aPieceka83b8e02023-06-07 15:25:16 +0200273 ly_set_erase(&yo->schema_features, free_features);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100274
aPieceka83b8e02023-06-07 15:25:16 +0200275 if (ly_ctx_new_ylpath(yo->searchpaths, yo->yang_lib_file, LYD_UNKNOWN, yo->ctx_options, ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -0400276 YLMSG_E("Unable to modify libyang context with yang-library data.\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100277 return -1;
278 }
279 } else {
280 /* set imp feature flag if all should be enabled */
aPieceka83b8e02023-06-07 15:25:16 +0200281 yo->ctx_options |= !yo->schema_features.count ? LY_CTX_ENABLE_IMP_FEATURES : 0;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100282
aPieceka83b8e02023-06-07 15:25:16 +0200283 if (ly_ctx_new(yo->searchpaths, yo->ctx_options, ctx)) {
aPiecek912f3d52022-10-12 10:02:33 +0200284 YLMSG_E("Unable to create libyang context\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100285 return -1;
286 }
aPiecek912f3d52022-10-12 10:02:33 +0200287 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100288
aPiecek912f3d52022-10-12 10:02:33 +0200289 /* set callback providing run-time extension instance data */
aPieceka83b8e02023-06-07 15:25:16 +0200290 if (yo->schema_context_filename) {
291 ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, yo->schema_context_filename);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100292 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100293
Radek Krejcie9f13b12020-11-09 17:42:04 +0100294 for (int i = 0; i < argc - optind; i++) {
295 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
aPieceka83b8e02023-06-07 15:25:16 +0200296 LYD_FORMAT format_data = yo->data_in_format;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100297
aPieceka83b8e02023-06-07 15:25:16 +0200298 filepath = argv[optind + i];
299
300 if (!filepath) {
301 goto error;
302 }
303 if (get_input(filepath, &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100304 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100305 }
306
307 if (format_schema) {
308 LY_ERR ret;
309 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 +0100310 const char **features;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100311
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100312 /* parse the input */
aPieceka83b8e02023-06-07 15:25:16 +0200313 if (parse_schema_path(filepath, &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100314 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100315 }
316
aPieceka6d45b52023-05-19 11:48:59 +0200317 mod = NULL;
aPieceka83b8e02023-06-07 15:25:16 +0200318 if (yo->yang_lib_file) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100319 /* just get the module, it should already be parsed */
aPieceka83b8e02023-06-07 15:25:16 +0200320 mod = ly_ctx_get_module_implemented(*ctx, module);
aPieceka6d45b52023-05-19 11:48:59 +0200321 }
322 if (!mod) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100323 /* add temporarily also the path of the module itself */
aPieceka83b8e02023-06-07 15:25:16 +0200324 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100325 path_unset = 0;
326 }
327
328 /* get features list for this module */
aPieceka83b8e02023-06-07 15:25:16 +0200329 if (!yo->schema_features.count) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100330 features = all_features;
331 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200332 get_features(&yo->schema_features, module, &features);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100333 }
334
335 /* parse module */
aPieceka83b8e02023-06-07 15:25:16 +0200336 ret = lys_parse(*ctx, in, format_schema, features, &mod);
337 ly_ctx_unset_searchdir_last(*ctx, path_unset);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100338 if (ret) {
aPieceka83b8e02023-06-07 15:25:16 +0200339 YLMSG_E("Parsing schema module \"%s\" failed.\n", filepath);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100340 goto error;
341 }
Michal Vasko703370c2021-10-19 14:07:16 +0200342 }
343
Radek Krejcie9f13b12020-11-09 17:42:04 +0100344 /* temporary cleanup */
345 free(dir);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100346 dir = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100347 free(module);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100348 module = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100349
aPieceka83b8e02023-06-07 15:25:16 +0200350 if (yo->schema_out_format || yo->feature_param_format) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100351 /* module will be printed */
aPieceka83b8e02023-06-07 15:25:16 +0200352 if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) {
353 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", filepath);
Radek Krejcif2afd672020-11-26 12:29:23 +0100354 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100355 }
356 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100357 } else if (format_data) {
aPieceka83b8e02023-06-07 15:25:16 +0200358 if (!fill_cmdline_file(&yo->data_inputs, in, filepath, format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100359 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100360 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100361 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100362 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100363
364 ly_in_free(in, 1);
365 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100366 }
367
Michal Vaskoc91fb122021-11-23 09:00:17 +0100368 /* check that all specified features were applied, apply now if possible */
aPieceka83b8e02023-06-07 15:25:16 +0200369 while ((sf = get_features_not_applied(&yo->schema_features))) {
Michal Vaskoc91fb122021-11-23 09:00:17 +0100370 /* try to find implemented or the latest revision of this module */
aPieceka83b8e02023-06-07 15:25:16 +0200371 mod = ly_ctx_get_module_implemented(*ctx, sf->mod_name);
Michal Vaskoc91fb122021-11-23 09:00:17 +0100372 if (!mod) {
aPieceka83b8e02023-06-07 15:25:16 +0200373 mod = ly_ctx_get_module_latest(*ctx, sf->mod_name);
Michal Vasko686d8fc2021-11-22 10:03:23 +0100374 }
Michal Vaskoc91fb122021-11-23 09:00:17 +0100375 if (!mod) {
376 YLMSG_E("Specified features not applied, module \"%s\" not loaded.\n", sf->mod_name);
377 goto error;
378 }
379
380 /* we have the module, implement it if needed and enable the specific features */
381 if (lys_set_implemented(mod, (const char **)sf->features)) {
382 YLMSG_E("Implementing module \"%s\" failed.\n", mod->name);
383 goto error;
384 }
385 sf->applied = 1;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100386 }
387
Radek Krejcie9f13b12020-11-09 17:42:04 +0100388 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100389
390error:
391 ly_in_free(in, 1);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100392 free(dir);
393 free(module);
Radek Krejcif2afd672020-11-26 12:29:23 +0100394 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100395}
396
397/**
398 * @brief Process command line options and store the settings into the context.
399 *
400 * return -1 in case of error;
401 * return 0 in case of success and ready to process
402 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200403 */
404static int
aPieceka83b8e02023-06-07 15:25:16 +0200405fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
Radek Krejcied5acc52019-04-25 15:57:04 +0200406{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100407 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200408
Radek Krejcie9f13b12020-11-09 17:42:04 +0100409 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200410 struct option options[] = {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100411 {"help", no_argument, NULL, 'h'},
412 {"version", no_argument, NULL, 'v'},
413 {"verbose", no_argument, NULL, 'V'},
414 {"quiet", no_argument, NULL, 'Q'},
415 {"format", required_argument, NULL, 'f'},
416 {"path", required_argument, NULL, 'p'},
Michal Vaskod8b0e772022-03-18 13:36:04 +0100417 {"disable-searchdir", no_argument, NULL, 'D'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100418 {"features", required_argument, NULL, 'F'},
419 {"make-implemented", no_argument, NULL, 'i'},
aPiecekef0a3392023-05-16 10:34:32 +0200420 {"in-format", required_argument, NULL, 'I'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100421 {"schema-node", required_argument, NULL, 'P'},
422 {"single-node", no_argument, NULL, 'q'},
423 {"submodule", required_argument, NULL, 's'},
ekinzie0ab8b302022-10-10 03:03:57 -0400424 {"ext-data", required_argument, NULL, 'x'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100425 {"not-strict", no_argument, NULL, 'n'},
426 {"present", no_argument, NULL, 'e'},
427 {"type", required_argument, NULL, 't'},
428 {"default", required_argument, NULL, 'd'},
aPiecek1b629762023-04-12 13:52:30 +0200429 {"data-xpath", required_argument, NULL, 'E'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100430 {"list", no_argument, NULL, 'l'},
431 {"tree-line-length", required_argument, NULL, 'L'},
432 {"output", required_argument, NULL, 'o'},
433 {"operational", required_argument, NULL, 'O'},
Michal Vasko3f08fb92022-04-21 09:52:35 +0200434 {"reply-rpc", required_argument, NULL, 'R'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100435 {"merge", no_argument, NULL, 'm'},
436 {"yang-library", no_argument, NULL, 'y'},
437 {"yang-library-file", required_argument, NULL, 'Y'},
stewegd8e2fc92023-05-31 09:52:56 +0200438 {"extended-leafref", no_argument, NULL, 'X'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200439#ifndef NDEBUG
440 {"debug", required_argument, NULL, 'G'},
441#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200442 {NULL, 0, NULL, 0}
443 };
Radek Krejcie9f13b12020-11-09 17:42:04 +0100444 uint8_t data_type_set = 0;
aPiecek7941e322023-05-10 15:44:41 +0200445 uint32_t temp_lo = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100446
aPieceka83b8e02023-06-07 15:25:16 +0200447 yo->ctx_options = YL_DEFAULT_CTX_OPTIONS;
448 yo->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
449 yo->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
450 yo->line_length = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100451
Michal Vasko27a4acb2021-11-22 10:03:46 +0100452 opterr = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200453#ifndef NDEBUG
aPiecekef0a3392023-05-16 10:34:32 +0200454 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 +0100455#else
aPiecekef0a3392023-05-16 10:34:32 +0200456 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 +0200457#endif
Michal Vasko03fe6202022-07-22 16:23:13 +0200458 {
Radek Krejcied5acc52019-04-25 15:57:04 +0200459 switch (opt) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200460 case 'h': /* --help */
461 help(0);
462 return 1;
463
464 case 'v': /* --version */
465 version();
466 return 1;
467
468 case 'V': { /* --verbose */
469 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100470
Michal Vaskoc40548b2021-09-30 13:05:47 +0200471 if (verbosity < LY_LLDBG) {
Michal Vaskob0d307b2021-11-25 11:15:16 +0100472 ++verbosity;
Radek Krejcied5acc52019-04-25 15:57:04 +0200473 }
Michal Vaskob0d307b2021-11-25 11:15:16 +0100474 ly_log_level(verbosity);
Radek Krejcied5acc52019-04-25 15:57:04 +0200475 break;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200476 } /* case 'V' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100477
Michal Vaskob0d307b2021-11-25 11:15:16 +0100478 case 'Q': { /* --quiet */
479 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100480
Michal Vaskob0d307b2021-11-25 11:15:16 +0100481 if (verbosity == LY_LLERR) {
482 /* turn logging off */
483 ly_log_options(LY_LOSTORE_LAST);
484 } else if (verbosity > LY_LLERR) {
485 --verbosity;
486 }
487 ly_log_level(verbosity);
488 break;
489 } /* case 'Q' */
490
Radek Krejcie9f13b12020-11-09 17:42:04 +0100491 case 'f': /* --format */
492 if (!strcasecmp(optarg, "yang")) {
aPieceka83b8e02023-06-07 15:25:16 +0200493 yo->schema_out_format = LYS_OUT_YANG;
494 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100495 } else if (!strcasecmp(optarg, "yin")) {
aPieceka83b8e02023-06-07 15:25:16 +0200496 yo->schema_out_format = LYS_OUT_YIN;
497 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100498 } else if (!strcasecmp(optarg, "info")) {
aPieceka83b8e02023-06-07 15:25:16 +0200499 yo->schema_out_format = LYS_OUT_YANG_COMPILED;
500 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100501 } else if (!strcasecmp(optarg, "tree")) {
aPieceka83b8e02023-06-07 15:25:16 +0200502 yo->schema_out_format = LYS_OUT_TREE;
503 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100504 } else if (!strcasecmp(optarg, "xml")) {
aPieceka83b8e02023-06-07 15:25:16 +0200505 yo->schema_out_format = 0;
506 yo->data_out_format = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100507 } else if (!strcasecmp(optarg, "json")) {
aPieceka83b8e02023-06-07 15:25:16 +0200508 yo->schema_out_format = 0;
509 yo->data_out_format = LYD_JSON;
Michal Vaskod8b0e772022-03-18 13:36:04 +0100510 } else if (!strcasecmp(optarg, "lyb")) {
aPieceka83b8e02023-06-07 15:25:16 +0200511 yo->schema_out_format = 0;
512 yo->data_out_format = LYD_LYB;
roman300b8782022-08-11 12:49:21 +0200513 } else if (!strcasecmp(optarg, "feature-param")) {
aPieceka83b8e02023-06-07 15:25:16 +0200514 yo->feature_param_format = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200515 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100516 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200517 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100518 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200519 }
520 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100521
aPiecekef0a3392023-05-16 10:34:32 +0200522 case 'I': /* --in-format */
523 if (!strcasecmp(optarg, "xml")) {
aPieceka83b8e02023-06-07 15:25:16 +0200524 yo->data_in_format = LYD_XML;
aPiecekef0a3392023-05-16 10:34:32 +0200525 } else if (!strcasecmp(optarg, "json")) {
aPieceka83b8e02023-06-07 15:25:16 +0200526 yo->data_in_format = LYD_JSON;
aPiecekef0a3392023-05-16 10:34:32 +0200527 } else if (!strcasecmp(optarg, "lyb")) {
aPieceka83b8e02023-06-07 15:25:16 +0200528 yo->data_in_format = LYD_LYB;
aPiecekef0a3392023-05-16 10:34:32 +0200529 } else {
530 YLMSG_E("Unknown input format %s\n", optarg);
531 help(1);
532 return -1;
533 }
534 break;
535
Michal Vaskoc40548b2021-09-30 13:05:47 +0200536 case 'p': { /* --path */
537 struct stat st;
538
539 if (stat(optarg, &st) == -1) {
540 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
541 return -1;
542 }
543 if (!S_ISDIR(st.st_mode)) {
544 YLMSG_E("Provided search path is not a directory.\n");
545 return -1;
546 }
547
aPieceka83b8e02023-06-07 15:25:16 +0200548 if (searchpath_strcat(&yo->searchpaths, optarg)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200549 YLMSG_E("Storing searchpath failed.\n");
550 return -1;
551 }
552
553 break;
554 } /* case 'p' */
555
556 case 'D': /* --disable-search */
aPieceka83b8e02023-06-07 15:25:16 +0200557 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200558 YLMSG_W("The -D option specified too many times.\n");
559 }
aPieceka83b8e02023-06-07 15:25:16 +0200560 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {
561 yo->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
562 yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200563 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200564 yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200565 }
566 break;
567
568 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +0200569 if (parse_features(optarg, &yo->schema_features)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200570 return -1;
571 }
572 break;
573
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200574 case 'i': /* --make-implemented */
aPieceka83b8e02023-06-07 15:25:16 +0200575 if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) {
576 yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
577 yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200578 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200579 yo->ctx_options |= LY_CTX_REF_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200580 }
581 break;
582
Radek Krejcie9f13b12020-11-09 17:42:04 +0100583 case 'P': /* --schema-node */
aPieceka83b8e02023-06-07 15:25:16 +0200584 yo->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200585 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100586
587 case 'q': /* --single-node */
aPieceka83b8e02023-06-07 15:25:16 +0200588 yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100589 break;
590
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200591 case 's': /* --submodule */
aPieceka83b8e02023-06-07 15:25:16 +0200592 yo->submodule = optarg;
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200593 break;
594
ekinzie0ab8b302022-10-10 03:03:57 -0400595 case 'x': /* --ext-data */
aPieceka83b8e02023-06-07 15:25:16 +0200596 yo->schema_context_filename = optarg;
ekinzie0ab8b302022-10-10 03:03:57 -0400597 break;
598
Michal Vasko667ce6b2021-01-25 15:00:27 +0100599 case 'n': /* --not-strict */
aPieceka83b8e02023-06-07 15:25:16 +0200600 yo->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100601 break;
602
603 case 'e': /* --present */
aPieceka83b8e02023-06-07 15:25:16 +0200604 yo->data_validate_options |= LYD_VALIDATE_PRESENT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100605 break;
606
607 case 't': /* --type */
608 if (data_type_set) {
609 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
610 return -1;
611 }
612
613 if (!strcasecmp(optarg, "config")) {
aPieceka83b8e02023-06-07 15:25:16 +0200614 yo->data_parse_options |= LYD_PARSE_NO_STATE;
615 yo->data_validate_options |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100616 } else if (!strcasecmp(optarg, "get")) {
aPieceka83b8e02023-06-07 15:25:16 +0200617 yo->data_parse_options |= LYD_PARSE_ONLY;
aPiecekbe6c0602023-04-18 11:29:09 +0200618 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
aPieceka83b8e02023-06-07 15:25:16 +0200619 yo->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200620 } else if (!strcasecmp(optarg, "rpc")) {
aPieceka83b8e02023-06-07 15:25:16 +0200621 yo->data_type = LYD_TYPE_RPC_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200622 } else if (!strcasecmp(optarg, "nc-rpc")) {
aPieceka83b8e02023-06-07 15:25:16 +0200623 yo->data_type = LYD_TYPE_RPC_NETCONF;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200624 } else if (!strcasecmp(optarg, "reply")) {
aPieceka83b8e02023-06-07 15:25:16 +0200625 yo->data_type = LYD_TYPE_REPLY_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200626 } else if (!strcasecmp(optarg, "nc-reply")) {
aPieceka83b8e02023-06-07 15:25:16 +0200627 yo->data_type = LYD_TYPE_REPLY_NETCONF;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200628 } else if (!strcasecmp(optarg, "notif")) {
aPieceka83b8e02023-06-07 15:25:16 +0200629 yo->data_type = LYD_TYPE_NOTIF_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200630 } else if (!strcasecmp(optarg, "nc-notif")) {
aPieceka83b8e02023-06-07 15:25:16 +0200631 yo->data_type = LYD_TYPE_NOTIF_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100632 } else if (!strcasecmp(optarg, "data")) {
633 /* default option */
634 } else {
635 YLMSG_E("Unknown data tree type %s\n", optarg);
636 help(1);
637 return -1;
638 }
639
640 data_type_set = 1;
641 break;
642
Michal Vaskoc40548b2021-09-30 13:05:47 +0200643 case 'd': /* --default */
644 if (!strcasecmp(optarg, "all")) {
aPieceka83b8e02023-06-07 15:25:16 +0200645 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200646 } else if (!strcasecmp(optarg, "all-tagged")) {
aPieceka83b8e02023-06-07 15:25:16 +0200647 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200648 } else if (!strcasecmp(optarg, "trim")) {
aPieceka83b8e02023-06-07 15:25:16 +0200649 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200650 } else if (!strcasecmp(optarg, "implicit-tagged")) {
aPieceka83b8e02023-06-07 15:25:16 +0200651 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200652 } else {
653 YLMSG_E("Unknown default mode %s\n", optarg);
654 help(1);
655 return -1;
656 }
657 break;
658
aPiecek1b629762023-04-12 13:52:30 +0200659 case 'E': /* --data-xpath */
aPieceka83b8e02023-06-07 15:25:16 +0200660 if (ly_set_add(&yo->data_xpath, optarg, 0, NULL)) {
aPiecek1b629762023-04-12 13:52:30 +0200661 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
662 return -1;
663 }
664 break;
665
Michal Vaskoc40548b2021-09-30 13:05:47 +0200666 case 'l': /* --list */
aPieceka83b8e02023-06-07 15:25:16 +0200667 yo->list = 1;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200668 break;
669
670 case 'L': /* --tree-line-length */
aPieceka83b8e02023-06-07 15:25:16 +0200671 yo->line_length = atoi(optarg);
Michal Vaskoc40548b2021-09-30 13:05:47 +0200672 break;
673
674 case 'o': /* --output */
aPieceka83b8e02023-06-07 15:25:16 +0200675 if (yo->out) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200676 YLMSG_E("Only a single output can be specified.\n");
677 return -1;
678 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200679 if (ly_out_new_filepath(optarg, &yo->out)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200680 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
681 return -1;
682 }
683 }
684 break;
685
Radek Krejcie9f13b12020-11-09 17:42:04 +0100686 case 'O': /* --operational */
aPieceka83b8e02023-06-07 15:25:16 +0200687 if (yo->data_operational.path) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100688 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
689 return -1;
690 }
aPieceka83b8e02023-06-07 15:25:16 +0200691 yo->data_operational.path = optarg;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100692 break;
693
Michal Vasko3f08fb92022-04-21 09:52:35 +0200694 case 'R': /* --reply-rpc */
aPieceka83b8e02023-06-07 15:25:16 +0200695 if (yo->reply_rpc.path) {
Michal Vasko3f08fb92022-04-21 09:52:35 +0200696 YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n");
697 return -1;
698 }
aPieceka83b8e02023-06-07 15:25:16 +0200699 yo->reply_rpc.path = optarg;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200700 break;
701
Radek Krejcie9f13b12020-11-09 17:42:04 +0100702 case 'm': /* --merge */
aPieceka83b8e02023-06-07 15:25:16 +0200703 yo->data_merge = 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100704 break;
705
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100706 case 'y': /* --yang-library */
aPieceka83b8e02023-06-07 15:25:16 +0200707 yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100708 break;
709
710 case 'Y': /* --yang-library-file */
aPieceka83b8e02023-06-07 15:25:16 +0200711 yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
712 yo->yang_lib_file = optarg;
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100713 break;
714
stewegd8e2fc92023-05-31 09:52:56 +0200715 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +0200716 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
stewegd8e2fc92023-05-31 09:52:56 +0200717 break;
718
Radek Krejcied5acc52019-04-25 15:57:04 +0200719#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100720 case 'G': { /* --debug */
721 uint32_t dbg_groups = 0;
722 const char *ptr = optarg;
723
Radek Krejcied5acc52019-04-25 15:57:04 +0200724 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100725 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100726 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100727 ptr += sizeof "dict" - 1;
728 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100729 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100730 ptr += sizeof "xpath" - 1;
Michal Vasko27a4acb2021-11-22 10:03:46 +0100731 } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {
732 dbg_groups |= LY_LDGDEPSETS;
733 ptr += sizeof "dep-sets" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200734 }
735
736 if (ptr[0]) {
737 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100738 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
739 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200740 }
741 ++ptr;
742 }
743 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100744 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200745 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100746 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200747#endif
Michal Vasko27a4acb2021-11-22 10:03:46 +0100748 default:
749 YLMSG_E("Invalid option or missing argument: -%c\n", optopt);
750 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100751 } /* switch */
752 }
753
Radek Krejcie9f13b12020-11-09 17:42:04 +0100754 /* additional checks for the options combinations */
aPieceka83b8e02023-06-07 15:25:16 +0200755 if (!yo->list && (optind >= argc)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100756 help(1);
757 YLMSG_E("Missing <schema> to process.\n");
758 return 1;
759 }
760
aPiecekbc39de12023-06-08 08:10:29 +0200761 if (cmd_data_dep(yo, 0)) {
Michal Vasko3f08fb92022-04-21 09:52:35 +0200762 return -1;
763 }
aPiecek04c8c2e2023-06-08 08:38:22 +0200764 if (cmd_print_dep(yo, 0)) {
765 return -1;
aPiecek2cfeeae2021-04-21 13:17:34 +0200766 }
767
aPieceka83b8e02023-06-07 15:25:16 +0200768 if (yo->schema_out_format == LYS_OUT_TREE) {
aPiecek7ac66d52021-05-20 07:54:07 +0200769 /* print tree from lysc_nodes */
aPieceka83b8e02023-06-07 15:25:16 +0200770 yo->ctx_options |= LY_CTX_SET_PRIV_PARSED;
aPiecek7ac66d52021-05-20 07:54:07 +0200771 }
772
Radek Krejcie9f13b12020-11-09 17:42:04 +0100773 /* process input files provided as standalone command line arguments,
774 * schema modules are parsed and inserted into the context,
775 * data files are just checked and prepared into internal structures for further processing */
aPieceka83b8e02023-06-07 15:25:16 +0200776 ret = fill_context_inputs(argc, argv, yo, ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100777 if (ret) {
778 return ret;
779 }
780
781 /* the second batch of checks */
aPieceka83b8e02023-06-07 15:25:16 +0200782 if (yo->schema_print_options && !yo->schema_out_format) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100783 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
784 }
aPieceka83b8e02023-06-07 15:25:16 +0200785 if (yo->schema_parse_options && !yo->schema_modules.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100786 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
787 }
aPieceka83b8e02023-06-07 15:25:16 +0200788 if (yo->data_print_options && !yo->data_out_format) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100789 YLMSG_W("data printer options specified, but the data output format is missing.\n");
790 }
aPieceka83b8e02023-06-07 15:25:16 +0200791 if (((yo->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || yo->data_type) && !yo->data_inputs.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100792 YLMSG_W("Data parser options specified, but no data input file provided.\n");
793 }
794
aPieceka83b8e02023-06-07 15:25:16 +0200795 if (yo->schema_node_path) {
aPiecek7941e322023-05-10 15:44:41 +0200796 /* turn off logging so that the message is not repeated */
797 ly_temp_log_options(&temp_lo);
798 /* search operation input */
aPieceka83b8e02023-06-07 15:25:16 +0200799 yo->schema_node = lys_find_path(*ctx, NULL, yo->schema_node_path, 0);
800 if (!yo->schema_node) {
aPiecek7941e322023-05-10 15:44:41 +0200801 /* restore logging so an error may be displayed */
802 ly_temp_log_options(NULL);
803 /* search operation output */
aPieceka83b8e02023-06-07 15:25:16 +0200804 yo->schema_node = lys_find_path(*ctx, NULL, yo->schema_node_path, 1);
805 if (!yo->schema_node) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100806 YLMSG_E("Invalid schema path.\n");
807 return -1;
808 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200809 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200810 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100811
Radek Krejcie9f13b12020-11-09 17:42:04 +0100812 return 0;
813}
814
815int
816main_ni(int argc, char *argv[])
817{
818 int ret = EXIT_SUCCESS, r;
aPieceka83b8e02023-06-07 15:25:16 +0200819 struct yl_opt yo = {0};
820 struct ly_ctx *ctx = NULL;
roman300b8782022-08-11 12:49:21 +0200821 char *features_output = NULL;
822 struct ly_set set = {0};
823 uint32_t u;
Radek Krejcied5acc52019-04-25 15:57:04 +0200824
825 /* set callback for printing libyang messages */
826 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100827
aPieceka83b8e02023-06-07 15:25:16 +0200828 r = fill_context(argc, argv, &yo, &ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100829 if (r < 0) {
830 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200831 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100832 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200833 goto cleanup;
834 }
835
Radek Krejcie9f13b12020-11-09 17:42:04 +0100836 /* do the required job - parse, validate, print */
837
aPieceka83b8e02023-06-07 15:25:16 +0200838 if (yo.list) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100839 /* print the list of schemas */
aPieceka83b8e02023-06-07 15:25:16 +0200840 ret = print_list(yo.out, ctx, yo.data_out_format);
aPiecek34262622023-03-27 08:28:25 +0200841 goto cleanup;
Radek Krejci047d64e2020-12-03 12:57:10 +0100842 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200843 if (yo.feature_param_format) {
844 for (u = 0; u < yo.schema_modules.count; u++) {
845 if (collect_features(yo.schema_modules.objs[u], &set)) {
roman300b8782022-08-11 12:49:21 +0200846 YLMSG_E("Unable to read features from a module.\n");
847 goto cleanup;
848 }
aPieceka83b8e02023-06-07 15:25:16 +0200849 if (generate_features_output(yo.schema_modules.objs[u], &set, &features_output)) {
roman300b8782022-08-11 12:49:21 +0200850 YLMSG_E("Unable to generate feature command output.\n");
851 goto cleanup;
852 }
853 ly_set_erase(&set, NULL);
854 }
aPieceka83b8e02023-06-07 15:25:16 +0200855 ly_print(yo.out, "%s\n", features_output);
856 } else if (yo.schema_out_format) {
857 if (yo.schema_node) {
858 ret = lys_print_node(yo.out, yo.schema_node, yo.schema_out_format, yo.line_length, yo.schema_print_options);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100859 if (ret) {
aPieceka83b8e02023-06-07 15:25:16 +0200860 YLMSG_E("Unable to print schema node %s.\n", yo.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100861 goto cleanup;
862 }
aPieceka83b8e02023-06-07 15:25:16 +0200863 } else if (yo.submodule) {
864 const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(ctx, yo.submodule);
Michal Vasko26bbb272022-08-02 14:54:33 +0200865
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200866 if (!submod) {
aPieceka83b8e02023-06-07 15:25:16 +0200867 YLMSG_E("Unable to find submodule %s.\n", yo.submodule);
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200868 goto cleanup;
869 }
870
aPieceka83b8e02023-06-07 15:25:16 +0200871 ret = lys_print_submodule(yo.out, submod, yo.schema_out_format, yo.line_length, yo.schema_print_options);
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200872 if (ret) {
873 YLMSG_E("Unable to print submodule %s.\n", submod->name);
874 goto cleanup;
875 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100876 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200877 for (u = 0; u < yo.schema_modules.count; ++u) {
878 ret = lys_print_module(yo.out, (struct lys_module *)yo.schema_modules.objs[u], yo.schema_out_format,
879 yo.line_length, yo.schema_print_options);
aPiecek8f0b7882021-05-21 10:18:36 +0200880 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
aPieceka83b8e02023-06-07 15:25:16 +0200881 if ((yo.schema_out_format == LYS_OUT_TREE) && (u + 1 < yo.schema_modules.count)) {
882 ly_print(yo.out, "\n");
aPiecek8f0b7882021-05-21 10:18:36 +0200883 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100884 if (ret) {
aPieceka83b8e02023-06-07 15:25:16 +0200885 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)yo.schema_modules.objs[u])->name);
Radek Krejci047d64e2020-12-03 12:57:10 +0100886 goto cleanup;
887 }
888 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100889 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200890 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100891
892 /* do the data validation despite the schema was printed */
aPieceka83b8e02023-06-07 15:25:16 +0200893 if (yo.data_inputs.size) {
894 ret = process_data(ctx, yo.data_type, yo.data_merge, yo.data_out_format, yo.out, yo.data_parse_options,
895 yo.data_validate_options, yo.data_print_options, &yo.data_operational, &yo.reply_rpc, &yo.data_inputs,
896 &yo.data_xpath);
Michal Vasko0f1e4562022-03-29 11:57:57 +0200897 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100898 goto cleanup;
899 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200900 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200901 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200902
903cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100904 /* cleanup */
aPieceka83b8e02023-06-07 15:25:16 +0200905 yl_opt_erase(&yo);
906 ly_ctx_destroy(ctx);
roman300b8782022-08-11 12:49:21 +0200907 free(features_output);
908 ly_set_erase(&set, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200909
Radek Krejcied5acc52019-04-25 15:57:04 +0200910 return ret;
911}