blob: 57a5eb5343fb81d8d4f879a6cc25d29aaeba0c52 [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
Radek Krejcie9f13b12020-11-09 17:42:04 +010030#include "common.h"
aPiecek8f0b7882021-05-21 10:18:36 +020031#include "out.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "tools/config.h"
aPieceka83b8e02023-06-07 15:25:16 +020033#include "yl_opt.h"
Radek Krejcie9f13b12020-11-09 17:42:04 +010034
35static void
36version(void)
37{
38 printf("yanglint %s\n", PROJECT_VERSION);
39}
40
41static void
Radek Krejcied5acc52019-04-25 15:57:04 +020042help(int shortout)
43{
Radek Krejcie9f13b12020-11-09 17:42:04 +010044
Michal Vasko3f08fb92022-04-21 09:52:35 +020045 printf("Example usage:\n"
46 " yanglint [-f { yang | yin | info}] <schema>...\n"
47 " Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n"
48 " them in the specified format.\n\n"
49 " yanglint [-f { xml | json }] <schema>... <file>...\n"
50 " Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n"
51 " printing them in the specified format.\n\n"
52 " yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n"
53 " Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n"
aPieceka40764b2023-04-27 15:34:56 +020054 " <operational-file> with possible references to the operational datastore data.\n"
55 " To validate nested-notification or action, the <operational-file> is required.\n\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +020056 " yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n"
57 " Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n"
58 " using <operational-file> with possible references to the operational datastore data.\n\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010059 " yanglint\n"
60 " Starts interactive mode with more features.\n\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020061
62 if (shortout) {
63 return;
64 }
Radek Krejcie9f13b12020-11-09 17:42:04 +010065 printf("Options:\n"
66 " -h, --help Show this help message and exit.\n"
67 " -v, --version Show version number and exit.\n"
Michal Vaskob0d307b2021-11-25 11:15:16 +010068 " -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n"
69 " a second time, show even debug messages.\n"
70 " -Q, --quiet Decrease libyang verbosity and hide warnings. If specified a second\n"
71 " time, hide errors so no libyang messages are printed.\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010072
Michal Vaskoc40548b2021-09-30 13:05:47 +020073 printf(" -f FORMAT, --format=FORMAT\n"
74 " Convert input into FORMAT. Supported formats: \n"
roman300b8782022-08-11 12:49:21 +020075 " yang, yin, tree, info and feature-param for schemas,\n"
Michal Vaskod8b0e772022-03-18 13:36:04 +010076 " xml, json, and lyb for data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +020077
aPiecekef0a3392023-05-16 10:34:32 +020078 printf(" -I FORMAT, --in-format=FORMAT\n"
79 " Load the data in one of the following formats:\n"
80 " xml, json, lyb\n"
81 " If input format not specified, it is detected from the file extension.\n\n");
82
Michal Vaskoc40548b2021-09-30 13:05:47 +020083 printf(" -p PATH, --path=PATH\n"
84 " Search path for schema (YANG/YIN) modules. The option can be\n"
85 " used multiple times. The current working directory and the\n"
aPiecek4f306da2023-03-27 09:06:07 +020086 " path of the module being added is used implicitly. Subdirectories\n"
87 " are also searched\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010088
Michal Vasko22e0f3a2021-09-22 12:19:23 +020089 printf(" -D, --disable-searchdir\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +010090 " Do not implicitly search in current working directory for\n"
91 " schema modules. If specified a second time, do not even\n"
92 " search in the module directory (all modules must be \n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +020093 " explicitly specified).\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +010094
Michal Vasko22e0f3a2021-09-22 12:19:23 +020095 printf(" -F FEATURES, --features=FEATURES\n"
Michal Vasko59740872021-11-22 10:01:34 +010096 " Specific module features to support in the form <module-name>:(<feature>,)*\n"
97 " Use <feature> '*' to enable all features of a module. This option can be\n"
98 " specified multiple times, to enable features in multiple modules. If this\n"
99 " option is not specified, all the features in all the implemented modules\n"
100 " are enabled.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100101
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200102 printf(" -i, --make-implemented\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100103 " Make the imported modules \"referenced\" from any loaded\n"
104 " module also implemented. If specified a second time, all the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200105 " modules are set implemented.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100106
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200107 printf(" -P PATH, --schema-node=PATH\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100108 " Print only the specified subtree of the schema.\n"
109 " The PATH is the XPath subset mentioned in documentation as\n"
110 " the Path format. The option can be combined with --single-node\n"
111 " option to print information only about the specified node.\n"
112 " -q, --single-node\n"
113 " Supplement to the --schema-node option to print information\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200114 " only about a single node specified as PATH argument.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100115
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200116 printf(" -s SUBMODULE, --submodule=SUBMODULE\n"
117 " Print the specific submodule instead of the main module.\n\n");
118
ekinzie0ab8b302022-10-10 03:03:57 -0400119 printf(" -x FILE, --ext-data=FILE\n"
120 " File containing the specific data required by an extension. Required by\n"
Michal Vasko14662802022-11-08 08:45:46 +0100121 " the schema-mount extension, for example, when the operational data are\n"
ekinzie0ab8b302022-10-10 03:03:57 -0400122 " expected in the file. File format is guessed.\n\n");
123
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200124 printf(" -n, --not-strict\n"
Michal Vasko667ce6b2021-01-25 15:00:27 +0100125 " Do not require strict data parsing (silently skip unknown data),\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200126 " has no effect for schemas.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100127
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200128 printf(" -e, --present Validate only with the schema modules whose data actually\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100129 " exist in the provided input data files. Takes effect only\n"
130 " with the 'data' or 'config' TYPEs. Used to avoid requiring\n"
131 " mandatory nodes from modules which data are not present in the\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200132 " provided input data files.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100133
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200134 printf(" -t TYPE, --type=TYPE\n"
Radek Krejcie9f13b12020-11-09 17:42:04 +0100135 " Specify data tree type in the input data file(s):\n"
136 " data - Complete datastore with status data (default type).\n"
137 " config - Configuration datastore (without status data).\n"
Michal Vasko3f08fb92022-04-21 09:52:35 +0200138 " get - Data returned by the NETCONF <get> operation.\n"
139 " getconfig - Data returned by the NETCONF <get-config> operation.\n"
140 " edit - Config content of the NETCONF <edit-config> operation.\n"
141 " rpc - Invocation of a YANG RPC/action, defined as input.\n"
142 " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n"
143 " envelopes <rpc> or <action>.\n"
144 " reply - Reply to a YANG RPC/action, defined as output. Note that\n"
145 " the reply data are expected inside a container representing\n"
146 " the original RPC/action invocation.\n"
147 " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n"
148 " envelope <rpc-reply> with output data nodes as direct\n"
149 " descendants. The original RPC/action invocation is expected\n"
150 " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n"
151 " notif - Notification instance of a YANG notification.\n"
152 " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n"
153 " envelope <notification> with element <eventTime> and its\n"
154 " sibling as the actual notification.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200155
156 printf(" -d MODE, --default=MODE\n"
157 " Print data with default values, according to the MODE\n"
158 " (to print attributes, ietf-netconf-with-defaults model\n"
159 " must be loaded):\n"
160 " all - Add missing default nodes.\n"
161 " all-tagged - Add missing default nodes and mark all the default\n"
162 " nodes with the attribute.\n"
163 " trim - Remove all nodes with a default value.\n"
164 " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n");
165
aPiecek1b629762023-04-12 13:52:30 +0200166 printf(" -E XPATH, --data-xpath=XPATH\n"
aPiecek41955272023-05-10 16:10:17 +0200167 " Evaluate XPATH expression over the data and print the nodes satisfying\n"
aPiecek1b629762023-04-12 13:52:30 +0200168 " the expression. The output format is specific and the option cannot\n"
169 " be combined with the -f and -d options. Also all the data\n"
170 " inputs are merged into a single data tree where the expression\n"
171 " is evaluated, so the -m option is always set implicitly.\n\n");
172
Michal Vaskoc40548b2021-09-30 13:05:47 +0200173 printf(" -l, --list Print info about the loaded schemas.\n"
174 " (i - imported module, I - implemented module)\n"
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100175 " In case the '-f' option with data encoding is specified,\n"
176 " the list is printed as \"ietf-yang-library\" data.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200177
178 printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n"
179 " The limit of the maximum line length on which the 'tree'\n"
180 " format will try to be printed.\n\n");
181
182 printf(" -o OUTFILE, --output=OUTFILE\n"
183 " Write the output to OUTFILE instead of stdout.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100184
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200185 printf(" -O FILE, --operational=FILE\n"
Michal Vasko9e81ce52022-04-29 10:16:21 +0200186 " Provide optional data to extend validation of the '(nc-)rpc',\n"
187 " '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n"
188 " the operational datastore referenced from the operation.\n"
aPieceka40764b2023-04-27 15:34:56 +0200189 " In case of a nested notification or action, its parent existence\n"
190 " is also checked in these operational data.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100191
Michal Vasko3f08fb92022-04-21 09:52:35 +0200192 printf(" -R FILE, --reply-rpc=FILE\n"
193 " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n"
194 " is supposed to contain the source 'nc-rpc' operation of the reply.\n\n");
195
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200196 printf(" -m, --merge Merge input data files into a single tree and validate at\n"
Michal Vasko06158fb2022-03-29 12:13:16 +0200197 " once. The option has effect only for 'data' and 'config' TYPEs.\n\n");
Radek Krejcie9f13b12020-11-09 17:42:04 +0100198
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200199 printf(" -y, --yang-library\n"
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100200 " Load and implement internal \"ietf-yang-library\" YANG module.\n"
201 " Note that this module includes definitions of mandatory state\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200202 " data that can result in unexpected data validation errors.\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200203
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100204 printf(" -Y FILE, --yang-library-file=FILE\n"
205 " Parse FILE with \"ietf-yang-library\" data and use them to\n"
206 " create an exact YANG schema context. If specified, the '-F'\n"
207 " parameter (enabled features) is ignored.\n\n");
208
stewegd8e2fc92023-05-31 09:52:56 +0200209 printf(" -X, --extended-leafref\n"
210 " Allow usage of deref() XPath function within leafref\n\n");
211
Michal Vaskoc40548b2021-09-30 13:05:47 +0200212#ifndef NDEBUG
213 printf(" -G GROUPS, --debug=GROUPS\n"
214 " Enable printing of specific debugging message group\n"
215 " (nothing will be printed unless verbosity is set to debug):\n"
Michal Vasko27a4acb2021-11-22 10:03:46 +0100216 " <group>[,<group>]* (dict, xpath, dep-sets)\n\n");
Michal Vaskoc40548b2021-09-30 13:05:47 +0200217#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200218}
219
Radek Krejcie9f13b12020-11-09 17:42:04 +0100220static void
Radek Krejcied5acc52019-04-25 15:57:04 +0200221libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path)
222{
223 char *levstr;
224
Radek Krejcie9f13b12020-11-09 17:42:04 +0100225 switch (level) {
226 case LY_LLERR:
227 levstr = "err :";
228 break;
229 case LY_LLWRN:
230 levstr = "warn:";
231 break;
232 case LY_LLVRB:
233 levstr = "verb:";
234 break;
235 default:
236 levstr = "dbg :";
237 break;
238 }
239 if (path) {
240 fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path);
241 } else {
242 fprintf(stderr, "libyang %s %s\n", levstr, msg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200243 }
244}
245
Michal Vasko686d8fc2021-11-22 10:03:23 +0100246static struct schema_features *
247get_features_not_applied(const struct ly_set *fset)
248{
249 for (uint32_t u = 0; u < fset->count; ++u) {
250 struct schema_features *sf = fset->objs[u];
Michal Vasko26bbb272022-08-02 14:54:33 +0200251
Michal Vasko686d8fc2021-11-22 10:03:23 +0100252 if (!sf->applied) {
253 return sf;
254 }
255 }
256
257 return NULL;
258}
259
Radek Krejcie9f13b12020-11-09 17:42:04 +0100260static int
aPieceka83b8e02023-06-07 15:25:16 +0200261fill_context_inputs(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
Radek Krejcie9f13b12020-11-09 17:42:04 +0100262{
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100263 struct ly_in *in = NULL;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100264 struct schema_features *sf;
Michal Vaskoc91fb122021-11-23 09:00:17 +0100265 struct lys_module *mod;
aPiecek912f3d52022-10-12 10:02:33 +0200266 const char *all_features[] = {"*", NULL};
aPieceka83b8e02023-06-07 15:25:16 +0200267 char *dir = NULL, *module = NULL, *filepath = NULL;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100268
aPiecek912f3d52022-10-12 10:02:33 +0200269 /* Create libyang context. */
aPieceka83b8e02023-06-07 15:25:16 +0200270 if (yo->yang_lib_file) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100271 /* ignore features */
aPieceka83b8e02023-06-07 15:25:16 +0200272 ly_set_erase(&yo->schema_features, free_features);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100273
aPieceka83b8e02023-06-07 15:25:16 +0200274 if (ly_ctx_new_ylpath(yo->searchpaths, yo->yang_lib_file, LYD_UNKNOWN, yo->ctx_options, ctx)) {
ekinzie0ab8b302022-10-10 03:03:57 -0400275 YLMSG_E("Unable to modify libyang context with yang-library data.\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100276 return -1;
277 }
278 } else {
279 /* set imp feature flag if all should be enabled */
aPieceka83b8e02023-06-07 15:25:16 +0200280 yo->ctx_options |= !yo->schema_features.count ? LY_CTX_ENABLE_IMP_FEATURES : 0;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100281
aPieceka83b8e02023-06-07 15:25:16 +0200282 if (ly_ctx_new(yo->searchpaths, yo->ctx_options, ctx)) {
aPiecek912f3d52022-10-12 10:02:33 +0200283 YLMSG_E("Unable to create libyang context\n");
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100284 return -1;
285 }
aPiecek912f3d52022-10-12 10:02:33 +0200286 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100287
aPiecek912f3d52022-10-12 10:02:33 +0200288 /* set callback providing run-time extension instance data */
aPieceka83b8e02023-06-07 15:25:16 +0200289 if (yo->schema_context_filename) {
290 ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, yo->schema_context_filename);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100291 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100292
Michal Vasko3f08fb92022-04-21 09:52:35 +0200293 /* process the operational and/or reply RPC content if any */
aPieceka83b8e02023-06-07 15:25:16 +0200294 if (yo->data_operational.path) {
295 if (get_input(yo->data_operational.path, NULL, &yo->data_operational.format, &yo->data_operational.in)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100296 return -1;
297 }
298 }
aPieceka83b8e02023-06-07 15:25:16 +0200299 if (yo->reply_rpc.path) {
300 if (get_input(yo->reply_rpc.path, NULL, &yo->reply_rpc.format, &yo->reply_rpc.in)) {
Michal Vasko3f08fb92022-04-21 09:52:35 +0200301 return -1;
302 }
303 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100304
305 for (int i = 0; i < argc - optind; i++) {
306 LYS_INFORMAT format_schema = LYS_IN_UNKNOWN;
aPieceka83b8e02023-06-07 15:25:16 +0200307 LYD_FORMAT format_data = yo->data_in_format;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100308
aPieceka83b8e02023-06-07 15:25:16 +0200309 filepath = argv[optind + i];
310
311 if (!filepath) {
312 goto error;
313 }
314 if (get_input(filepath, &format_schema, &format_data, &in)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100315 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100316 }
317
318 if (format_schema) {
319 LY_ERR ret;
320 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 +0100321 const char **features;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100322
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100323 /* parse the input */
aPieceka83b8e02023-06-07 15:25:16 +0200324 if (parse_schema_path(filepath, &dir, &module)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100325 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100326 }
327
aPieceka6d45b52023-05-19 11:48:59 +0200328 mod = NULL;
aPieceka83b8e02023-06-07 15:25:16 +0200329 if (yo->yang_lib_file) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100330 /* just get the module, it should already be parsed */
aPieceka83b8e02023-06-07 15:25:16 +0200331 mod = ly_ctx_get_module_implemented(*ctx, module);
aPieceka6d45b52023-05-19 11:48:59 +0200332 }
333 if (!mod) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100334 /* add temporarily also the path of the module itself */
aPieceka83b8e02023-06-07 15:25:16 +0200335 if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100336 path_unset = 0;
337 }
338
339 /* get features list for this module */
aPieceka83b8e02023-06-07 15:25:16 +0200340 if (!yo->schema_features.count) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100341 features = all_features;
342 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200343 get_features(&yo->schema_features, module, &features);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100344 }
345
346 /* parse module */
aPieceka83b8e02023-06-07 15:25:16 +0200347 ret = lys_parse(*ctx, in, format_schema, features, &mod);
348 ly_ctx_unset_searchdir_last(*ctx, path_unset);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100349 if (ret) {
aPieceka83b8e02023-06-07 15:25:16 +0200350 YLMSG_E("Parsing schema module \"%s\" failed.\n", filepath);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100351 goto error;
352 }
Michal Vasko703370c2021-10-19 14:07:16 +0200353 }
354
Radek Krejcie9f13b12020-11-09 17:42:04 +0100355 /* temporary cleanup */
356 free(dir);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100357 dir = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100358 free(module);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100359 module = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100360
aPieceka83b8e02023-06-07 15:25:16 +0200361 if (yo->schema_out_format || yo->feature_param_format) {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100362 /* module will be printed */
aPieceka83b8e02023-06-07 15:25:16 +0200363 if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) {
364 YLMSG_E("Storing parsed schema module (%s) for print failed.\n", filepath);
Radek Krejcif2afd672020-11-26 12:29:23 +0100365 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100366 }
367 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100368 } else if (format_data) {
aPieceka83b8e02023-06-07 15:25:16 +0200369 if (!fill_cmdline_file(&yo->data_inputs, in, filepath, format_data)) {
Radek Krejcif2afd672020-11-26 12:29:23 +0100370 goto error;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100371 }
Radek Krejcif53b0d52020-12-03 11:29:24 +0100372 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100373 }
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100374
375 ly_in_free(in, 1);
376 in = NULL;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100377 }
378
Michal Vaskoc91fb122021-11-23 09:00:17 +0100379 /* check that all specified features were applied, apply now if possible */
aPieceka83b8e02023-06-07 15:25:16 +0200380 while ((sf = get_features_not_applied(&yo->schema_features))) {
Michal Vaskoc91fb122021-11-23 09:00:17 +0100381 /* try to find implemented or the latest revision of this module */
aPieceka83b8e02023-06-07 15:25:16 +0200382 mod = ly_ctx_get_module_implemented(*ctx, sf->mod_name);
Michal Vaskoc91fb122021-11-23 09:00:17 +0100383 if (!mod) {
aPieceka83b8e02023-06-07 15:25:16 +0200384 mod = ly_ctx_get_module_latest(*ctx, sf->mod_name);
Michal Vasko686d8fc2021-11-22 10:03:23 +0100385 }
Michal Vaskoc91fb122021-11-23 09:00:17 +0100386 if (!mod) {
387 YLMSG_E("Specified features not applied, module \"%s\" not loaded.\n", sf->mod_name);
388 goto error;
389 }
390
391 /* we have the module, implement it if needed and enable the specific features */
392 if (lys_set_implemented(mod, (const char **)sf->features)) {
393 YLMSG_E("Implementing module \"%s\" failed.\n", mod->name);
394 goto error;
395 }
396 sf->applied = 1;
Michal Vasko686d8fc2021-11-22 10:03:23 +0100397 }
398
Radek Krejcie9f13b12020-11-09 17:42:04 +0100399 return 0;
Radek Krejcif2afd672020-11-26 12:29:23 +0100400
401error:
402 ly_in_free(in, 1);
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100403 free(dir);
404 free(module);
Radek Krejcif2afd672020-11-26 12:29:23 +0100405 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100406}
407
408/**
409 * @brief Process command line options and store the settings into the context.
410 *
411 * return -1 in case of error;
412 * return 0 in case of success and ready to process
413 * return 1 in case of success, but expect to exit.
Radek Krejcied5acc52019-04-25 15:57:04 +0200414 */
415static int
aPieceka83b8e02023-06-07 15:25:16 +0200416fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
Radek Krejcied5acc52019-04-25 15:57:04 +0200417{
Radek Krejcie9f13b12020-11-09 17:42:04 +0100418 int ret;
Radek Krejcie7b95092019-05-15 11:03:07 +0200419
Radek Krejcie9f13b12020-11-09 17:42:04 +0100420 int opt, opt_index;
Radek Krejcied5acc52019-04-25 15:57:04 +0200421 struct option options[] = {
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100422 {"help", no_argument, NULL, 'h'},
423 {"version", no_argument, NULL, 'v'},
424 {"verbose", no_argument, NULL, 'V'},
425 {"quiet", no_argument, NULL, 'Q'},
426 {"format", required_argument, NULL, 'f'},
427 {"path", required_argument, NULL, 'p'},
Michal Vaskod8b0e772022-03-18 13:36:04 +0100428 {"disable-searchdir", no_argument, NULL, 'D'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100429 {"features", required_argument, NULL, 'F'},
430 {"make-implemented", no_argument, NULL, 'i'},
aPiecekef0a3392023-05-16 10:34:32 +0200431 {"in-format", required_argument, NULL, 'I'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100432 {"schema-node", required_argument, NULL, 'P'},
433 {"single-node", no_argument, NULL, 'q'},
434 {"submodule", required_argument, NULL, 's'},
ekinzie0ab8b302022-10-10 03:03:57 -0400435 {"ext-data", required_argument, NULL, 'x'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100436 {"not-strict", no_argument, NULL, 'n'},
437 {"present", no_argument, NULL, 'e'},
438 {"type", required_argument, NULL, 't'},
439 {"default", required_argument, NULL, 'd'},
aPiecek1b629762023-04-12 13:52:30 +0200440 {"data-xpath", required_argument, NULL, 'E'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100441 {"list", no_argument, NULL, 'l'},
442 {"tree-line-length", required_argument, NULL, 'L'},
443 {"output", required_argument, NULL, 'o'},
444 {"operational", required_argument, NULL, 'O'},
Michal Vasko3f08fb92022-04-21 09:52:35 +0200445 {"reply-rpc", required_argument, NULL, 'R'},
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100446 {"merge", no_argument, NULL, 'm'},
447 {"yang-library", no_argument, NULL, 'y'},
448 {"yang-library-file", required_argument, NULL, 'Y'},
stewegd8e2fc92023-05-31 09:52:56 +0200449 {"extended-leafref", no_argument, NULL, 'X'},
Michal Vaskoc40548b2021-09-30 13:05:47 +0200450#ifndef NDEBUG
451 {"debug", required_argument, NULL, 'G'},
452#endif
Radek Krejcied5acc52019-04-25 15:57:04 +0200453 {NULL, 0, NULL, 0}
454 };
Radek Krejcie9f13b12020-11-09 17:42:04 +0100455 uint8_t data_type_set = 0;
aPiecek7941e322023-05-10 15:44:41 +0200456 uint32_t temp_lo = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100457
aPieceka83b8e02023-06-07 15:25:16 +0200458 yo->ctx_options = YL_DEFAULT_CTX_OPTIONS;
459 yo->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS;
460 yo->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS;
461 yo->line_length = 0;
Michal Vasko667ce6b2021-01-25 15:00:27 +0100462
Michal Vasko27a4acb2021-11-22 10:03:46 +0100463 opterr = 0;
Radek Krejcied5acc52019-04-25 15:57:04 +0200464#ifndef NDEBUG
aPiecekef0a3392023-05-16 10:34:32 +0200465 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 +0100466#else
aPiecekef0a3392023-05-16 10:34:32 +0200467 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 +0200468#endif
Michal Vasko03fe6202022-07-22 16:23:13 +0200469 {
Radek Krejcied5acc52019-04-25 15:57:04 +0200470 switch (opt) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200471 case 'h': /* --help */
472 help(0);
473 return 1;
474
475 case 'v': /* --version */
476 version();
477 return 1;
478
479 case 'V': { /* --verbose */
480 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100481
Michal Vaskoc40548b2021-09-30 13:05:47 +0200482 if (verbosity < LY_LLDBG) {
Michal Vaskob0d307b2021-11-25 11:15:16 +0100483 ++verbosity;
Radek Krejcied5acc52019-04-25 15:57:04 +0200484 }
Michal Vaskob0d307b2021-11-25 11:15:16 +0100485 ly_log_level(verbosity);
Radek Krejcied5acc52019-04-25 15:57:04 +0200486 break;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200487 } /* case 'V' */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100488
Michal Vaskob0d307b2021-11-25 11:15:16 +0100489 case 'Q': { /* --quiet */
490 LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100491
Michal Vaskob0d307b2021-11-25 11:15:16 +0100492 if (verbosity == LY_LLERR) {
493 /* turn logging off */
494 ly_log_options(LY_LOSTORE_LAST);
495 } else if (verbosity > LY_LLERR) {
496 --verbosity;
497 }
498 ly_log_level(verbosity);
499 break;
500 } /* case 'Q' */
501
Radek Krejcie9f13b12020-11-09 17:42:04 +0100502 case 'f': /* --format */
503 if (!strcasecmp(optarg, "yang")) {
aPieceka83b8e02023-06-07 15:25:16 +0200504 yo->schema_out_format = LYS_OUT_YANG;
505 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100506 } else if (!strcasecmp(optarg, "yin")) {
aPieceka83b8e02023-06-07 15:25:16 +0200507 yo->schema_out_format = LYS_OUT_YIN;
508 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100509 } else if (!strcasecmp(optarg, "info")) {
aPieceka83b8e02023-06-07 15:25:16 +0200510 yo->schema_out_format = LYS_OUT_YANG_COMPILED;
511 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100512 } else if (!strcasecmp(optarg, "tree")) {
aPieceka83b8e02023-06-07 15:25:16 +0200513 yo->schema_out_format = LYS_OUT_TREE;
514 yo->data_out_format = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100515 } else if (!strcasecmp(optarg, "xml")) {
aPieceka83b8e02023-06-07 15:25:16 +0200516 yo->schema_out_format = 0;
517 yo->data_out_format = LYD_XML;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100518 } else if (!strcasecmp(optarg, "json")) {
aPieceka83b8e02023-06-07 15:25:16 +0200519 yo->schema_out_format = 0;
520 yo->data_out_format = LYD_JSON;
Michal Vaskod8b0e772022-03-18 13:36:04 +0100521 } else if (!strcasecmp(optarg, "lyb")) {
aPieceka83b8e02023-06-07 15:25:16 +0200522 yo->schema_out_format = 0;
523 yo->data_out_format = LYD_LYB;
roman300b8782022-08-11 12:49:21 +0200524 } else if (!strcasecmp(optarg, "feature-param")) {
aPieceka83b8e02023-06-07 15:25:16 +0200525 yo->feature_param_format = 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200526 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100527 YLMSG_E("Unknown output format %s\n", optarg);
Radek Krejcied5acc52019-04-25 15:57:04 +0200528 help(1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100529 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200530 }
531 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100532
aPiecekef0a3392023-05-16 10:34:32 +0200533 case 'I': /* --in-format */
534 if (!strcasecmp(optarg, "xml")) {
aPieceka83b8e02023-06-07 15:25:16 +0200535 yo->data_in_format = LYD_XML;
aPiecekef0a3392023-05-16 10:34:32 +0200536 } else if (!strcasecmp(optarg, "json")) {
aPieceka83b8e02023-06-07 15:25:16 +0200537 yo->data_in_format = LYD_JSON;
aPiecekef0a3392023-05-16 10:34:32 +0200538 } else if (!strcasecmp(optarg, "lyb")) {
aPieceka83b8e02023-06-07 15:25:16 +0200539 yo->data_in_format = LYD_LYB;
aPiecekef0a3392023-05-16 10:34:32 +0200540 } else {
541 YLMSG_E("Unknown input format %s\n", optarg);
542 help(1);
543 return -1;
544 }
545 break;
546
Michal Vaskoc40548b2021-09-30 13:05:47 +0200547 case 'p': { /* --path */
548 struct stat st;
549
550 if (stat(optarg, &st) == -1) {
551 YLMSG_E("Unable to use search path (%s) - %s.\n", optarg, strerror(errno));
552 return -1;
553 }
554 if (!S_ISDIR(st.st_mode)) {
555 YLMSG_E("Provided search path is not a directory.\n");
556 return -1;
557 }
558
aPieceka83b8e02023-06-07 15:25:16 +0200559 if (searchpath_strcat(&yo->searchpaths, optarg)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200560 YLMSG_E("Storing searchpath failed.\n");
561 return -1;
562 }
563
564 break;
565 } /* case 'p' */
566
567 case 'D': /* --disable-search */
aPieceka83b8e02023-06-07 15:25:16 +0200568 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200569 YLMSG_W("The -D option specified too many times.\n");
570 }
aPieceka83b8e02023-06-07 15:25:16 +0200571 if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {
572 yo->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
573 yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200574 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200575 yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200576 }
577 break;
578
579 case 'F': /* --features */
aPieceka83b8e02023-06-07 15:25:16 +0200580 if (parse_features(optarg, &yo->schema_features)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200581 return -1;
582 }
583 break;
584
Michal Vasko8d6d0ad2021-10-19 12:32:27 +0200585 case 'i': /* --make-implemented */
aPieceka83b8e02023-06-07 15:25:16 +0200586 if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) {
587 yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
588 yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200589 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200590 yo->ctx_options |= LY_CTX_REF_IMPLEMENTED;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200591 }
592 break;
593
Radek Krejcie9f13b12020-11-09 17:42:04 +0100594 case 'P': /* --schema-node */
aPieceka83b8e02023-06-07 15:25:16 +0200595 yo->schema_node_path = optarg;
Radek Krejcied5acc52019-04-25 15:57:04 +0200596 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100597
598 case 'q': /* --single-node */
aPieceka83b8e02023-06-07 15:25:16 +0200599 yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100600 break;
601
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200602 case 's': /* --submodule */
aPieceka83b8e02023-06-07 15:25:16 +0200603 yo->submodule = optarg;
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200604 break;
605
ekinzie0ab8b302022-10-10 03:03:57 -0400606 case 'x': /* --ext-data */
aPieceka83b8e02023-06-07 15:25:16 +0200607 yo->schema_context_filename = optarg;
ekinzie0ab8b302022-10-10 03:03:57 -0400608 break;
609
Michal Vasko667ce6b2021-01-25 15:00:27 +0100610 case 'n': /* --not-strict */
aPieceka83b8e02023-06-07 15:25:16 +0200611 yo->data_parse_options &= ~LYD_PARSE_STRICT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100612 break;
613
614 case 'e': /* --present */
aPieceka83b8e02023-06-07 15:25:16 +0200615 yo->data_validate_options |= LYD_VALIDATE_PRESENT;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100616 break;
617
618 case 't': /* --type */
619 if (data_type_set) {
620 YLMSG_E("The data type (-t) cannot be set multiple times.\n");
621 return -1;
622 }
623
624 if (!strcasecmp(optarg, "config")) {
aPieceka83b8e02023-06-07 15:25:16 +0200625 yo->data_parse_options |= LYD_PARSE_NO_STATE;
626 yo->data_validate_options |= LYD_VALIDATE_NO_STATE;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100627 } else if (!strcasecmp(optarg, "get")) {
aPieceka83b8e02023-06-07 15:25:16 +0200628 yo->data_parse_options |= LYD_PARSE_ONLY;
aPiecekbe6c0602023-04-18 11:29:09 +0200629 } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) {
aPieceka83b8e02023-06-07 15:25:16 +0200630 yo->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200631 } else if (!strcasecmp(optarg, "rpc")) {
aPieceka83b8e02023-06-07 15:25:16 +0200632 yo->data_type = LYD_TYPE_RPC_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200633 } else if (!strcasecmp(optarg, "nc-rpc")) {
aPieceka83b8e02023-06-07 15:25:16 +0200634 yo->data_type = LYD_TYPE_RPC_NETCONF;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200635 } else if (!strcasecmp(optarg, "reply")) {
aPieceka83b8e02023-06-07 15:25:16 +0200636 yo->data_type = LYD_TYPE_REPLY_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200637 } else if (!strcasecmp(optarg, "nc-reply")) {
aPieceka83b8e02023-06-07 15:25:16 +0200638 yo->data_type = LYD_TYPE_REPLY_NETCONF;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200639 } else if (!strcasecmp(optarg, "notif")) {
aPieceka83b8e02023-06-07 15:25:16 +0200640 yo->data_type = LYD_TYPE_NOTIF_YANG;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200641 } else if (!strcasecmp(optarg, "nc-notif")) {
aPieceka83b8e02023-06-07 15:25:16 +0200642 yo->data_type = LYD_TYPE_NOTIF_NETCONF;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100643 } else if (!strcasecmp(optarg, "data")) {
644 /* default option */
645 } else {
646 YLMSG_E("Unknown data tree type %s\n", optarg);
647 help(1);
648 return -1;
649 }
650
651 data_type_set = 1;
652 break;
653
Michal Vaskoc40548b2021-09-30 13:05:47 +0200654 case 'd': /* --default */
655 if (!strcasecmp(optarg, "all")) {
aPieceka83b8e02023-06-07 15:25:16 +0200656 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200657 } else if (!strcasecmp(optarg, "all-tagged")) {
aPieceka83b8e02023-06-07 15:25:16 +0200658 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200659 } else if (!strcasecmp(optarg, "trim")) {
aPieceka83b8e02023-06-07 15:25:16 +0200660 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200661 } else if (!strcasecmp(optarg, "implicit-tagged")) {
aPieceka83b8e02023-06-07 15:25:16 +0200662 yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200663 } else {
664 YLMSG_E("Unknown default mode %s\n", optarg);
665 help(1);
666 return -1;
667 }
668 break;
669
aPiecek1b629762023-04-12 13:52:30 +0200670 case 'E': /* --data-xpath */
aPieceka83b8e02023-06-07 15:25:16 +0200671 if (ly_set_add(&yo->data_xpath, optarg, 0, NULL)) {
aPiecek1b629762023-04-12 13:52:30 +0200672 YLMSG_E("Storing XPath \"%s\" failed.\n", optarg);
673 return -1;
674 }
675 break;
676
Michal Vaskoc40548b2021-09-30 13:05:47 +0200677 case 'l': /* --list */
aPieceka83b8e02023-06-07 15:25:16 +0200678 yo->list = 1;
Michal Vaskoc40548b2021-09-30 13:05:47 +0200679 break;
680
681 case 'L': /* --tree-line-length */
aPieceka83b8e02023-06-07 15:25:16 +0200682 yo->line_length = atoi(optarg);
Michal Vaskoc40548b2021-09-30 13:05:47 +0200683 break;
684
685 case 'o': /* --output */
aPieceka83b8e02023-06-07 15:25:16 +0200686 if (yo->out) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200687 YLMSG_E("Only a single output can be specified.\n");
688 return -1;
689 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200690 if (ly_out_new_filepath(optarg, &yo->out)) {
Michal Vaskoc40548b2021-09-30 13:05:47 +0200691 YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno));
692 return -1;
693 }
694 }
695 break;
696
Radek Krejcie9f13b12020-11-09 17:42:04 +0100697 case 'O': /* --operational */
aPieceka83b8e02023-06-07 15:25:16 +0200698 if (yo->data_operational.path) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100699 YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n");
700 return -1;
701 }
aPieceka83b8e02023-06-07 15:25:16 +0200702 yo->data_operational.path = optarg;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100703 break;
704
Michal Vasko3f08fb92022-04-21 09:52:35 +0200705 case 'R': /* --reply-rpc */
aPieceka83b8e02023-06-07 15:25:16 +0200706 if (yo->reply_rpc.path) {
Michal Vasko3f08fb92022-04-21 09:52:35 +0200707 YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n");
708 return -1;
709 }
aPieceka83b8e02023-06-07 15:25:16 +0200710 yo->reply_rpc.path = optarg;
Michal Vasko3f08fb92022-04-21 09:52:35 +0200711 break;
712
Radek Krejcie9f13b12020-11-09 17:42:04 +0100713 case 'm': /* --merge */
aPieceka83b8e02023-06-07 15:25:16 +0200714 yo->data_merge = 1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100715 break;
716
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100717 case 'y': /* --yang-library */
aPieceka83b8e02023-06-07 15:25:16 +0200718 yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
Michal Vasko24aaf8e2022-02-04 11:15:26 +0100719 break;
720
721 case 'Y': /* --yang-library-file */
aPieceka83b8e02023-06-07 15:25:16 +0200722 yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY;
723 yo->yang_lib_file = optarg;
Michal Vaskoc431a0a2021-01-25 14:31:58 +0100724 break;
725
stewegd8e2fc92023-05-31 09:52:56 +0200726 case 'X': /* --extended-leafref */
aPieceka83b8e02023-06-07 15:25:16 +0200727 yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED;
stewegd8e2fc92023-05-31 09:52:56 +0200728 break;
729
Radek Krejcied5acc52019-04-25 15:57:04 +0200730#ifndef NDEBUG
Radek Krejcie9f13b12020-11-09 17:42:04 +0100731 case 'G': { /* --debug */
732 uint32_t dbg_groups = 0;
733 const char *ptr = optarg;
734
Radek Krejcied5acc52019-04-25 15:57:04 +0200735 while (ptr[0]) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100736 if (!strncasecmp(ptr, "dict", sizeof "dict" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100737 dbg_groups |= LY_LDGDICT;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100738 ptr += sizeof "dict" - 1;
739 } else if (!strncasecmp(ptr, "xpath", sizeof "xpath" - 1)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100740 dbg_groups |= LY_LDGXPATH;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100741 ptr += sizeof "xpath" - 1;
Michal Vasko27a4acb2021-11-22 10:03:46 +0100742 } else if (!strncasecmp(ptr, "dep-sets", sizeof "dep-sets" - 1)) {
743 dbg_groups |= LY_LDGDEPSETS;
744 ptr += sizeof "dep-sets" - 1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200745 }
746
747 if (ptr[0]) {
748 if (ptr[0] != ',') {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100749 YLMSG_E("Unknown debug group string \"%s\"\n", optarg);
750 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200751 }
752 ++ptr;
753 }
754 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100755 ly_log_dbg_groups(dbg_groups);
Radek Krejcied5acc52019-04-25 15:57:04 +0200756 break;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100757 } /* case 'G' */
Radek Krejcied5acc52019-04-25 15:57:04 +0200758#endif
Michal Vasko27a4acb2021-11-22 10:03:46 +0100759 default:
760 YLMSG_E("Invalid option or missing argument: -%c\n", optopt);
761 return -1;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100762 } /* switch */
763 }
764
Radek Krejcie9f13b12020-11-09 17:42:04 +0100765 /* additional checks for the options combinations */
aPieceka83b8e02023-06-07 15:25:16 +0200766 if (!yo->list && (optind >= argc)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100767 help(1);
768 YLMSG_E("Missing <schema> to process.\n");
769 return 1;
770 }
771
aPieceka83b8e02023-06-07 15:25:16 +0200772 if (yo->data_xpath.count) {
773 yo->data_merge = 1;
aPiecek1b629762023-04-12 13:52:30 +0200774 }
aPieceka83b8e02023-06-07 15:25:16 +0200775 if (yo->data_xpath.count && (yo->schema_out_format || yo->data_out_format)) {
aPiecek1b629762023-04-12 13:52:30 +0200776 YLMSG_E("The --format option cannot be combined with --data-xpath option.\n");
777 return -1;
778 }
aPieceka83b8e02023-06-07 15:25:16 +0200779 if (yo->data_xpath.count && (yo->data_print_options & LYD_PRINT_WD_MASK)) {
aPiecek1b629762023-04-12 13:52:30 +0200780 YLMSG_E("The --default option cannot be combined with --data-xpath option.\n");
781 return -1;
782 }
783
aPieceka83b8e02023-06-07 15:25:16 +0200784 if (yo->data_merge) {
785 if (yo->data_type || (yo->data_parse_options & LYD_PARSE_ONLY)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100786 /* switch off the option, incompatible input data type */
aPiecek3d46fa92023-05-10 08:30:39 +0200787 YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs\n");
aPieceka83b8e02023-06-07 15:25:16 +0200788 yo->data_merge = 0;
Radek Krejcie9f13b12020-11-09 17:42:04 +0100789 } else {
790 /* postpone validation after the merge of all the input data */
aPieceka83b8e02023-06-07 15:25:16 +0200791 yo->data_parse_options |= LYD_PARSE_ONLY;
Radek Krejcied5acc52019-04-25 15:57:04 +0200792 }
793 }
794
aPieceka83b8e02023-06-07 15:25:16 +0200795 if (yo->data_operational.path && !yo->data_type) {
aPiecekce481582023-04-26 15:44:59 +0200796 YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notification input data types.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200797 yo->data_operational.path = NULL;
Radek Krejcied5acc52019-04-25 15:57:04 +0200798 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100799
aPieceka83b8e02023-06-07 15:25:16 +0200800 if (yo->reply_rpc.path && (yo->data_type != LYD_TYPE_REPLY_NETCONF)) {
aPiecekce481582023-04-26 15:44:59 +0200801 YLMSG_W("Source RPC is needed only for NETCONF Reply input data type.\n");
aPieceka83b8e02023-06-07 15:25:16 +0200802 yo->data_operational.path = NULL;
803 } else if (!yo->reply_rpc.path && (yo->data_type == LYD_TYPE_REPLY_NETCONF)) {
Michal Vasko3f08fb92022-04-21 09:52:35 +0200804 YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\n");
805 return -1;
806 }
807
aPieceka83b8e02023-06-07 15:25:16 +0200808 if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) {
aPiecekce481582023-04-26 15:44:59 +0200809 YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n");
aPiecek2cfeeae2021-04-21 13:17:34 +0200810 }
811
aPieceka83b8e02023-06-07 15:25:16 +0200812 if (!yo->out && (yo->data_out_format == LYD_LYB)) {
aPieceka4723482023-05-11 14:31:02 +0200813 YLMSG_E("The LYB format requires the -o option specified.\n");
814 return -1;
815 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100816 /* default output stream */
aPieceka83b8e02023-06-07 15:25:16 +0200817 if (!yo->out) {
818 if (ly_out_new_file(stdout, &yo->out)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100819 YLMSG_E("Unable to set stdout as output.\n");
820 return -1;
Radek Krejcied5acc52019-04-25 15:57:04 +0200821 }
822 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100823
aPieceka83b8e02023-06-07 15:25:16 +0200824 if (yo->schema_out_format == LYS_OUT_TREE) {
aPiecek7ac66d52021-05-20 07:54:07 +0200825 /* print tree from lysc_nodes */
aPieceka83b8e02023-06-07 15:25:16 +0200826 yo->ctx_options |= LY_CTX_SET_PRIV_PARSED;
aPiecek7ac66d52021-05-20 07:54:07 +0200827 }
828
Radek Krejcie9f13b12020-11-09 17:42:04 +0100829 /* process input files provided as standalone command line arguments,
830 * schema modules are parsed and inserted into the context,
831 * data files are just checked and prepared into internal structures for further processing */
aPieceka83b8e02023-06-07 15:25:16 +0200832 ret = fill_context_inputs(argc, argv, yo, ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100833 if (ret) {
834 return ret;
835 }
836
837 /* the second batch of checks */
aPieceka83b8e02023-06-07 15:25:16 +0200838 if (yo->schema_print_options && !yo->schema_out_format) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100839 YLMSG_W("Schema printer options specified, but the schema output format is missing.\n");
840 }
aPieceka83b8e02023-06-07 15:25:16 +0200841 if (yo->schema_parse_options && !yo->schema_modules.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100842 YLMSG_W("Schema parser options specified, but no schema input file provided.\n");
843 }
aPieceka83b8e02023-06-07 15:25:16 +0200844 if (yo->data_print_options && !yo->data_out_format) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100845 YLMSG_W("data printer options specified, but the data output format is missing.\n");
846 }
aPieceka83b8e02023-06-07 15:25:16 +0200847 if (((yo->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || yo->data_type) && !yo->data_inputs.count) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100848 YLMSG_W("Data parser options specified, but no data input file provided.\n");
849 }
850
aPieceka83b8e02023-06-07 15:25:16 +0200851 if (yo->schema_node_path) {
aPiecek7941e322023-05-10 15:44:41 +0200852 /* turn off logging so that the message is not repeated */
853 ly_temp_log_options(&temp_lo);
854 /* search operation input */
aPieceka83b8e02023-06-07 15:25:16 +0200855 yo->schema_node = lys_find_path(*ctx, NULL, yo->schema_node_path, 0);
856 if (!yo->schema_node) {
aPiecek7941e322023-05-10 15:44:41 +0200857 /* restore logging so an error may be displayed */
858 ly_temp_log_options(NULL);
859 /* search operation output */
aPieceka83b8e02023-06-07 15:25:16 +0200860 yo->schema_node = lys_find_path(*ctx, NULL, yo->schema_node_path, 1);
861 if (!yo->schema_node) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100862 YLMSG_E("Invalid schema path.\n");
863 return -1;
864 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200865 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200866 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100867
Radek Krejcie9f13b12020-11-09 17:42:04 +0100868 return 0;
869}
870
871int
872main_ni(int argc, char *argv[])
873{
874 int ret = EXIT_SUCCESS, r;
aPieceka83b8e02023-06-07 15:25:16 +0200875 struct yl_opt yo = {0};
876 struct ly_ctx *ctx = NULL;
roman300b8782022-08-11 12:49:21 +0200877 char *features_output = NULL;
878 struct ly_set set = {0};
879 uint32_t u;
Radek Krejcied5acc52019-04-25 15:57:04 +0200880
881 /* set callback for printing libyang messages */
882 ly_set_log_clb(libyang_verbclb, 1);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100883
aPieceka83b8e02023-06-07 15:25:16 +0200884 r = fill_context(argc, argv, &yo, &ctx);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100885 if (r < 0) {
886 ret = EXIT_FAILURE;
Radek Krejcied5acc52019-04-25 15:57:04 +0200887 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100888 if (r) {
Radek Krejcied5acc52019-04-25 15:57:04 +0200889 goto cleanup;
890 }
891
Radek Krejcie9f13b12020-11-09 17:42:04 +0100892 /* do the required job - parse, validate, print */
893
aPieceka83b8e02023-06-07 15:25:16 +0200894 if (yo.list) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100895 /* print the list of schemas */
aPieceka83b8e02023-06-07 15:25:16 +0200896 ret = print_list(yo.out, ctx, yo.data_out_format);
aPiecek34262622023-03-27 08:28:25 +0200897 goto cleanup;
Radek Krejci047d64e2020-12-03 12:57:10 +0100898 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200899 if (yo.feature_param_format) {
900 for (u = 0; u < yo.schema_modules.count; u++) {
901 if (collect_features(yo.schema_modules.objs[u], &set)) {
roman300b8782022-08-11 12:49:21 +0200902 YLMSG_E("Unable to read features from a module.\n");
903 goto cleanup;
904 }
aPieceka83b8e02023-06-07 15:25:16 +0200905 if (generate_features_output(yo.schema_modules.objs[u], &set, &features_output)) {
roman300b8782022-08-11 12:49:21 +0200906 YLMSG_E("Unable to generate feature command output.\n");
907 goto cleanup;
908 }
909 ly_set_erase(&set, NULL);
910 }
aPieceka83b8e02023-06-07 15:25:16 +0200911 ly_print(yo.out, "%s\n", features_output);
912 } else if (yo.schema_out_format) {
913 if (yo.schema_node) {
914 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 +0100915 if (ret) {
aPieceka83b8e02023-06-07 15:25:16 +0200916 YLMSG_E("Unable to print schema node %s.\n", yo.schema_node_path);
Radek Krejcie9f13b12020-11-09 17:42:04 +0100917 goto cleanup;
918 }
aPieceka83b8e02023-06-07 15:25:16 +0200919 } else if (yo.submodule) {
920 const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(ctx, yo.submodule);
Michal Vasko26bbb272022-08-02 14:54:33 +0200921
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200922 if (!submod) {
aPieceka83b8e02023-06-07 15:25:16 +0200923 YLMSG_E("Unable to find submodule %s.\n", yo.submodule);
Michal Vaskofe74d1e2021-09-30 13:17:36 +0200924 goto cleanup;
925 }
926
aPieceka83b8e02023-06-07 15:25:16 +0200927 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 +0200928 if (ret) {
929 YLMSG_E("Unable to print submodule %s.\n", submod->name);
930 goto cleanup;
931 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100932 } else {
aPieceka83b8e02023-06-07 15:25:16 +0200933 for (u = 0; u < yo.schema_modules.count; ++u) {
934 ret = lys_print_module(yo.out, (struct lys_module *)yo.schema_modules.objs[u], yo.schema_out_format,
935 yo.line_length, yo.schema_print_options);
aPiecek8f0b7882021-05-21 10:18:36 +0200936 /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */
aPieceka83b8e02023-06-07 15:25:16 +0200937 if ((yo.schema_out_format == LYS_OUT_TREE) && (u + 1 < yo.schema_modules.count)) {
938 ly_print(yo.out, "\n");
aPiecek8f0b7882021-05-21 10:18:36 +0200939 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100940 if (ret) {
aPieceka83b8e02023-06-07 15:25:16 +0200941 YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)yo.schema_modules.objs[u])->name);
Radek Krejci047d64e2020-12-03 12:57:10 +0100942 goto cleanup;
943 }
944 }
Radek Krejcie9f13b12020-11-09 17:42:04 +0100945 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200946 }
Radek Krejci047d64e2020-12-03 12:57:10 +0100947
948 /* do the data validation despite the schema was printed */
aPieceka83b8e02023-06-07 15:25:16 +0200949 if (yo.data_inputs.size) {
950 ret = process_data(ctx, yo.data_type, yo.data_merge, yo.data_out_format, yo.out, yo.data_parse_options,
951 yo.data_validate_options, yo.data_print_options, &yo.data_operational, &yo.reply_rpc, &yo.data_inputs,
952 &yo.data_xpath);
Michal Vasko0f1e4562022-03-29 11:57:57 +0200953 if (ret) {
Radek Krejci047d64e2020-12-03 12:57:10 +0100954 goto cleanup;
955 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200956 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200957 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200958
959cleanup:
Radek Krejcie9f13b12020-11-09 17:42:04 +0100960 /* cleanup */
aPieceka83b8e02023-06-07 15:25:16 +0200961 yl_opt_erase(&yo);
962 ly_ctx_destroy(ctx);
roman300b8782022-08-11 12:49:21 +0200963 free(features_output);
964 ly_set_erase(&set, NULL);
Radek Krejcied5acc52019-04-25 15:57:04 +0200965
Radek Krejcied5acc52019-04-25 15:57:04 +0200966 return ret;
967}