Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_data.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 5 | * @brief 'data' command of the libyang's yanglint tool. |
| 6 | * |
| 7 | * Copyright (c) 2015-2020 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "cmd.h" |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <getopt.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <strings.h> |
| 26 | |
| 27 | #include "libyang.h" |
| 28 | |
| 29 | #include "common.h" |
| 30 | |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 31 | static void |
| 32 | cmd_data_help_header(void) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 33 | { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 34 | printf("Usage: data [-emn] [-t TYPE]\n" |
| 35 | " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n" |
| 36 | " data [-n] -t (rpc | notif | reply) [-O FILE]\n" |
| 37 | " [-F FORMAT] [-f FORMAT] [-d DEFAULTS] [-o OUTFILE] <data1> ...\n" |
| 38 | " data [-en] [-t TYPE] [-F FORMAT] -x XPATH [-o OUTFILE] <data1> ...\n" |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 39 | " Parse, validate and optionally print data instances\n"); |
| 40 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 41 | |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 42 | static void |
| 43 | cmd_data_help_type(void) |
| 44 | { |
| 45 | printf(" -t TYPE, --type=TYPE\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 46 | " Specify data tree type in the input data file(s):\n" |
| 47 | " data - Complete datastore with status data (default type).\n" |
| 48 | " config - Configuration datastore (without status data).\n" |
| 49 | " get - Result of the NETCONF <get> operation.\n" |
| 50 | " getconfig - Result of the NETCONF <get-config> operation.\n" |
| 51 | " edit - Content of the NETCONF <edit-config> operation.\n" |
| 52 | " rpc - Content of the NETCONF <rpc> message, defined as YANG's\n" |
| 53 | " RPC/Action input statement.\n" |
aPiecek | 860e34f | 2023-04-28 10:01:32 +0200 | [diff] [blame] | 54 | " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n" |
| 55 | " envelopes <rpc> or <action>.\n" |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 56 | " reply - Reply to the RPC/Action. Note that the reply data are\n" |
| 57 | " expected inside a container representing the original\n" |
| 58 | " RPC/Action. This is necessary to identify appropriate\n" |
| 59 | " data definitions in the schema module.\n" |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 60 | " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n" |
| 61 | " envelope <rpc-reply> with output data nodes as direct\n" |
| 62 | " descendants. The original RPC/action invocation is expected\n" |
| 63 | " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 64 | " notif - Notification instance (content of the <notification>\n" |
aPiecek | 6d2c1e7 | 2023-04-28 15:54:48 +0200 | [diff] [blame] | 65 | " element without <eventTime>).\n" |
| 66 | " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n" |
| 67 | " envelope <notification> with element <eventTime> and its\n" |
| 68 | " sibling as the actual notification.\n"); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 69 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 70 | |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 71 | static void |
| 72 | cmd_data_help_format(void) |
| 73 | { |
| 74 | printf(" -f FORMAT, --format=FORMAT\n" |
| 75 | " Print the data in one of the following formats:\n" |
| 76 | " xml, json, lyb\n" |
| 77 | " Note that the LYB format requires the -o option specified.\n"); |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | cmd_data_help_in_format(void) |
| 82 | { |
| 83 | printf(" -F FORMAT, --in-format=FORMAT\n" |
| 84 | " Load the data in one of the following formats:\n" |
| 85 | " xml, json, lyb\n" |
| 86 | " If input format not specified, it is detected from the file extension.\n"); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | cmd_data_help_default(void) |
| 91 | { |
| 92 | printf(" -d MODE, --default=MODE\n" |
| 93 | " Print data with default values, according to the MODE\n" |
| 94 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 95 | " must be loaded):\n" |
| 96 | " all - Add missing default nodes.\n" |
| 97 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 98 | " nodes with the attribute.\n" |
| 99 | " trim - Remove all nodes with a default value.\n" |
| 100 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n"); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | cmd_data_help_xpath(void) |
| 105 | { |
| 106 | printf(" -x XPATH, --xpath=XPATH\n" |
aPiecek | 4195527 | 2023-05-10 16:10:17 +0200 | [diff] [blame] | 107 | " Evaluate XPATH expression and print the nodes satisfying the\n" |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 108 | " expression. The output format is specific and the option cannot\n" |
| 109 | " be combined with the -f and -d options. Also all the data\n" |
| 110 | " inputs are merged into a single data tree where the expression\n" |
| 111 | " is evaluated, so the -m option is always set implicitly.\n"); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | cmd_data_help(void) |
| 116 | { |
| 117 | cmd_data_help_header(); |
| 118 | printf("\n"); |
| 119 | cmd_data_help_type(); |
| 120 | printf(" -e, --present Validate only with the schema modules whose data actually\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 121 | " exist in the provided input data files. Takes effect only\n" |
| 122 | " with the 'data' or 'config' TYPEs. Used to avoid requiring\n" |
| 123 | " mandatory nodes from modules which data are not present in the\n" |
| 124 | " provided input data files.\n" |
| 125 | " -m, --merge Merge input data files into a single tree and validate at\n" |
| 126 | " once.The option has effect only for 'data' and 'config' TYPEs.\n" |
| 127 | " In case of using -x option, the data are always merged.\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 128 | " -n, --not-strict\n" |
| 129 | " Do not require strict data parsing (silently skip unknown data),\n" |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 130 | " has no effect for schemas.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 131 | " -O FILE, --operational=FILE\n" |
| 132 | " Provide optional data to extend validation of the 'rpc',\n" |
| 133 | " 'reply' or 'notif' TYPEs. The FILE is supposed to contain\n" |
aPiecek | a40764b | 2023-04-27 15:34:56 +0200 | [diff] [blame] | 134 | " the operational datastore referenced from the operation.\n" |
| 135 | " In case of a nested notification or action, its parent\n" |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 136 | " existence is also checked in these operational data.\n" |
| 137 | " -R FILE, --reply-rpc=FILE\n" |
| 138 | " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n" |
| 139 | " is supposed to contain the source 'nc-rpc' operation of the reply.\n"); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 140 | cmd_data_help_format(); |
| 141 | cmd_data_help_in_format(); |
| 142 | printf(" -o OUTFILE, --output=OUTFILE\n" |
| 143 | " Write the output to OUTFILE instead of stdout.\n"); |
| 144 | cmd_data_help_xpath(); |
| 145 | printf("\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 148 | void |
| 149 | cmd_data(struct ly_ctx **ctx, const char *cmdline) |
| 150 | { |
| 151 | int argc = 0; |
| 152 | char **argv = NULL; |
| 153 | int opt, opt_index; |
| 154 | struct option options[] = { |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 155 | {"defaults", required_argument, NULL, 'd'}, |
| 156 | {"present", no_argument, NULL, 'e'}, |
| 157 | {"format", required_argument, NULL, 'f'}, |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 158 | {"in-format", required_argument, NULL, 'F'}, |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 159 | {"help", no_argument, NULL, 'h'}, |
| 160 | {"merge", no_argument, NULL, 'm'}, |
| 161 | {"output", required_argument, NULL, 'o'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 162 | {"operational", required_argument, NULL, 'O'}, |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 163 | {"reply-rpc", required_argument, NULL, 'R'}, |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 164 | {"not-strict", no_argument, NULL, 'n'}, |
| 165 | {"type", required_argument, NULL, 't'}, |
| 166 | {"xpath", required_argument, NULL, 'x'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 167 | {NULL, 0, NULL, 0} |
| 168 | }; |
| 169 | |
| 170 | uint8_t data_merge = 0; |
| 171 | uint32_t options_print = 0; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 172 | uint32_t options_parse = YL_DEFAULT_DATA_PARSE_OPTIONS; |
Michal Vasko | 05eaf83 | 2023-02-10 09:21:46 +0100 | [diff] [blame] | 173 | uint32_t options_validate = YL_DEFAULT_DATA_VALIDATE_OPTIONS; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 174 | enum lyd_type data_type = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 175 | uint8_t data_type_set = 0; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 176 | LYD_FORMAT outformat = LYD_UNKNOWN; |
| 177 | LYD_FORMAT informat = LYD_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 178 | struct ly_out *out = NULL; |
| 179 | struct cmdline_file *operational = NULL; |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 180 | struct cmdline_file *reply_rpc = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 181 | struct ly_set inputs = {0}; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 182 | struct ly_set xpaths = {0}; |
| 183 | |
| 184 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 185 | goto cleanup; |
| 186 | } |
| 187 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 188 | while ((opt = getopt_long(argc, argv, commands[CMD_DATA].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 189 | switch (opt) { |
| 190 | case 'd': /* --default */ |
| 191 | if (!strcasecmp(optarg, "all")) { |
| 192 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL; |
| 193 | } else if (!strcasecmp(optarg, "all-tagged")) { |
| 194 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG; |
| 195 | } else if (!strcasecmp(optarg, "trim")) { |
| 196 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM; |
| 197 | } else if (!strcasecmp(optarg, "implicit-tagged")) { |
| 198 | options_print = (options_print & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG; |
| 199 | } else { |
| 200 | YLMSG_E("Unknown default mode %s\n", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 201 | cmd_data_help_default(); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 202 | goto cleanup; |
| 203 | } |
| 204 | break; |
| 205 | case 'f': /* --format */ |
| 206 | if (!strcasecmp(optarg, "xml")) { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 207 | outformat = LYD_XML; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 208 | } else if (!strcasecmp(optarg, "json")) { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 209 | outformat = LYD_JSON; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 210 | } else if (!strcasecmp(optarg, "lyb")) { |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 211 | outformat = LYD_LYB; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 212 | } else { |
| 213 | YLMSG_E("Unknown output format %s\n", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 214 | cmd_data_help_format(); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 215 | goto cleanup; |
| 216 | } |
| 217 | break; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 218 | case 'F': /* --in-format */ |
| 219 | if (!strcasecmp(optarg, "xml")) { |
| 220 | informat = LYD_XML; |
| 221 | } else if (!strcasecmp(optarg, "json")) { |
| 222 | informat = LYD_JSON; |
| 223 | } else if (!strcasecmp(optarg, "lyb")) { |
| 224 | informat = LYD_LYB; |
| 225 | } else { |
| 226 | YLMSG_E("Unknown input format %s\n", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 227 | cmd_data_help_in_format(); |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 228 | goto cleanup; |
| 229 | } |
| 230 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 231 | case 'o': /* --output */ |
| 232 | if (out) { |
| 233 | YLMSG_E("Only a single output can be specified.\n"); |
| 234 | goto cleanup; |
| 235 | } else { |
| 236 | if (ly_out_new_filepath(optarg, &out)) { |
| 237 | YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 238 | goto cleanup; |
| 239 | } |
| 240 | } |
| 241 | break; |
| 242 | case 'O': { /* --operational */ |
| 243 | struct ly_in *in; |
aPiecek | 90c8516 | 2023-04-26 14:30:00 +0200 | [diff] [blame] | 244 | LYD_FORMAT f = LYD_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 245 | |
| 246 | if (operational) { |
| 247 | YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n"); |
| 248 | goto cleanup; |
| 249 | } |
| 250 | if (get_input(optarg, NULL, &f, &in)) { |
| 251 | goto cleanup; |
| 252 | } |
| 253 | operational = fill_cmdline_file(NULL, in, optarg, f); |
| 254 | break; |
| 255 | } /* case 'O' */ |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 256 | case 'R': { /* --reply-rpc */ |
| 257 | struct ly_in *in; |
| 258 | LYD_FORMAT f = LYD_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 259 | |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 260 | if (reply_rpc) { |
| 261 | YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n"); |
| 262 | goto cleanup; |
| 263 | } |
| 264 | if (get_input(optarg, NULL, &f, &in)) { |
| 265 | goto cleanup; |
| 266 | } |
| 267 | reply_rpc = fill_cmdline_file(NULL, in, optarg, f); |
| 268 | break; |
| 269 | } /* case 'R' */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 270 | case 'e': /* --present */ |
| 271 | options_validate |= LYD_VALIDATE_PRESENT; |
| 272 | break; |
| 273 | case 'm': /* --merge */ |
| 274 | data_merge = 1; |
| 275 | break; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 276 | case 'n': /* --not-strict */ |
| 277 | options_parse &= ~LYD_PARSE_STRICT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 278 | break; |
| 279 | case 't': /* --type */ |
| 280 | if (data_type_set) { |
| 281 | YLMSG_E("The data type (-t) cannot be set multiple times.\n"); |
| 282 | goto cleanup; |
| 283 | } |
| 284 | |
| 285 | if (!strcasecmp(optarg, "config")) { |
| 286 | options_parse |= LYD_PARSE_NO_STATE; |
Michal Vasko | f6bda33 | 2021-02-01 08:59:03 +0100 | [diff] [blame] | 287 | options_validate |= LYD_VALIDATE_NO_STATE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 288 | } else if (!strcasecmp(optarg, "get")) { |
| 289 | options_parse |= LYD_PARSE_ONLY; |
Michal Vasko | 6e98538 | 2022-07-13 18:34:57 +0200 | [diff] [blame] | 290 | } else if (!strcasecmp(optarg, "getconfig") || !strcasecmp(optarg, "get-config") || !strcasecmp(optarg, "edit")) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 291 | options_parse |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 292 | } else if (!strcasecmp(optarg, "rpc") || !strcasecmp(optarg, "action")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 293 | data_type = LYD_TYPE_RPC_YANG; |
aPiecek | 860e34f | 2023-04-28 10:01:32 +0200 | [diff] [blame] | 294 | } else if (!strcasecmp(optarg, "nc-rpc")) { |
| 295 | data_type = LYD_TYPE_RPC_NETCONF; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 296 | } else if (!strcasecmp(optarg, "reply") || !strcasecmp(optarg, "rpcreply")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 297 | data_type = LYD_TYPE_REPLY_YANG; |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 298 | } else if (!strcasecmp(optarg, "nc-reply")) { |
| 299 | data_type = LYD_TYPE_REPLY_NETCONF; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 300 | } else if (!strcasecmp(optarg, "notif") || !strcasecmp(optarg, "notification")) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 301 | data_type = LYD_TYPE_NOTIF_YANG; |
aPiecek | 6d2c1e7 | 2023-04-28 15:54:48 +0200 | [diff] [blame] | 302 | } else if (!strcasecmp(optarg, "nc-notif")) { |
| 303 | data_type = LYD_TYPE_NOTIF_NETCONF; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 304 | } else if (!strcasecmp(optarg, "data")) { |
| 305 | /* default option */ |
| 306 | } else { |
| 307 | YLMSG_E("Unknown data tree type %s.\n", optarg); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 308 | cmd_data_help_type(); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 309 | goto cleanup; |
| 310 | } |
| 311 | |
| 312 | data_type_set = 1; |
| 313 | break; |
| 314 | |
| 315 | case 'x': /* --xpath */ |
| 316 | if (ly_set_add(&xpaths, optarg, 0, NULL)) { |
| 317 | YLMSG_E("Storing XPath \"%s\" failed.\n", optarg); |
| 318 | goto cleanup; |
| 319 | } |
| 320 | break; |
| 321 | |
| 322 | case 'h': /* --help */ |
| 323 | cmd_data_help(); |
| 324 | goto cleanup; |
| 325 | default: |
| 326 | YLMSG_E("Unknown option.\n"); |
| 327 | goto cleanup; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (optind == argc) { |
| 332 | YLMSG_E("Missing the data file to process.\n"); |
| 333 | goto cleanup; |
| 334 | } |
| 335 | |
| 336 | if (data_merge) { |
| 337 | if (data_type || (options_parse & LYD_PARSE_ONLY)) { |
| 338 | /* switch off the option, incompatible input data type */ |
aPiecek | 3d46fa9 | 2023-05-10 08:30:39 +0200 | [diff] [blame] | 339 | YLMSG_W("The --merge option has effect only for 'data' and 'config' TYPEs\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 340 | data_merge = 0; |
| 341 | } else { |
| 342 | /* postpone validation after the merge of all the input data */ |
| 343 | options_parse |= LYD_PARSE_ONLY; |
| 344 | } |
| 345 | } else if (xpaths.count) { |
| 346 | data_merge = 1; |
| 347 | } |
| 348 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 349 | if (xpaths.count && outformat) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 350 | YLMSG_E("The --format option cannot be combined with --xpath option.\n"); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 351 | cmd_data_help_xpath(); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 352 | goto cleanup; |
| 353 | } |
aPiecek | 72a24c9 | 2023-04-12 15:03:05 +0200 | [diff] [blame] | 354 | if (xpaths.count && (options_print & LYD_PRINT_WD_MASK)) { |
| 355 | YLMSG_E("The --default option cannot be combined with --xpath option.\n"); |
aPiecek | b073f6a | 2023-04-28 15:23:25 +0200 | [diff] [blame] | 356 | cmd_data_help_xpath(); |
aPiecek | 72a24c9 | 2023-04-12 15:03:05 +0200 | [diff] [blame] | 357 | goto cleanup; |
| 358 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 359 | |
| 360 | if (operational && !data_type) { |
| 361 | YLMSG_W("Operational datastore takes effect only with RPCs/Actions/Replies/Notifications input data types.\n"); |
| 362 | free_cmdline_file(operational); |
| 363 | operational = NULL; |
| 364 | } |
| 365 | |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 366 | if (reply_rpc && (data_type != LYD_TYPE_REPLY_NETCONF)) { |
| 367 | YLMSG_W("Source RPC is needed only for NETCONF Reply input data type.\n"); |
| 368 | free_cmdline_file(operational); |
| 369 | operational = NULL; |
| 370 | } else if (!reply_rpc && (data_type == LYD_TYPE_REPLY_NETCONF)) { |
| 371 | YLMSG_E("Missing source RPC (-R) for NETCONF Reply input data type.\n"); |
| 372 | goto cleanup; |
| 373 | } |
| 374 | |
aPiecek | a472348 | 2023-05-11 14:31:02 +0200 | [diff] [blame^] | 375 | if (!out && (outformat == LYD_LYB)) { |
| 376 | YLMSG_E("The LYB format requires the -o option specified.\n"); |
| 377 | goto cleanup; |
| 378 | } |
| 379 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 380 | /* process input data files provided as standalone command line arguments */ |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 381 | for (int i = 0; i < argc - optind; i++) { |
| 382 | struct ly_in *in; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 383 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 384 | if (get_input(argv[optind + i], NULL, &informat, &in)) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 385 | goto cleanup; |
| 386 | } |
| 387 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 388 | if (!fill_cmdline_file(&inputs, in, argv[optind + i], informat)) { |
Radek Krejci | 6784a4e | 2020-12-09 14:23:05 +0100 | [diff] [blame] | 389 | ly_in_free(in, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 390 | goto cleanup; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* default output stream */ |
| 395 | if (!out) { |
| 396 | if (ly_out_new_file(stdout, &out)) { |
| 397 | YLMSG_E("Unable to set stdout as output.\n"); |
| 398 | goto cleanup; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /* parse, validate and print data */ |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 403 | if (process_data(*ctx, data_type, data_merge, outformat, out, options_parse, options_validate, options_print, |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 404 | operational, reply_rpc, &inputs, &xpaths)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 405 | goto cleanup; |
| 406 | } |
| 407 | |
| 408 | cleanup: |
| 409 | ly_out_free(out, NULL, 0); |
| 410 | ly_set_erase(&inputs, free_cmdline_file); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 411 | ly_set_erase(&xpaths, NULL); |
| 412 | free_cmdline_file(operational); |
| 413 | free_cmdline(argv); |
| 414 | } |