Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main_ni.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool - noninteractive code |
| 5 | * |
| 6 | * Copyright (c) 2015-2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "config.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <getopt.h> |
| 20 | #include <errno.h> |
| 21 | #include <libgen.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/times.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "commands.h" |
| 29 | #include "libyang.h" |
| 30 | |
| 31 | volatile uint8_t verbose = 0; |
| 32 | |
| 33 | #if 0 |
| 34 | /* from commands.c */ |
| 35 | int print_list(FILE *out, struct ly_ctx *ctx, LYD_FORMAT outformat); |
| 36 | #endif |
| 37 | |
| 38 | void |
| 39 | help(int shortout) |
| 40 | { |
| 41 | fprintf(stdout, "Usage:\n"); |
| 42 | fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | jsons}] <file>...\n"); |
| 43 | fprintf(stdout, " Validates the YANG module in <file>, and all its dependencies.\n\n"); |
| 44 | fprintf(stdout, " yanglint [options] [-f { xml | json }] <schema>... <file>...\n"); |
| 45 | fprintf(stdout, " Validates the YANG modeled data in <file> according to the <schema>.\n\n"); |
| 46 | fprintf(stdout, " yanglint\n"); |
| 47 | fprintf(stdout, " Starts interactive mode with more features.\n\n"); |
| 48 | |
| 49 | if (shortout) { |
| 50 | return; |
| 51 | } |
| 52 | fprintf(stdout, "Options:\n" |
| 53 | " -h, --help Show this help message and exit.\n" |
| 54 | " -v, --version Show version number and exit.\n" |
| 55 | " -V, --verbose Show verbose messages, can be used multiple times to\n" |
| 56 | " increase verbosity.\n" |
| 57 | #ifndef NDEBUG |
| 58 | " -G GROUPS, --debug=GROUPS\n" |
| 59 | " Enable printing of specific debugging message group\n" |
| 60 | " (nothing will be printed unless verbosity is set to debug):\n" |
| 61 | " <group>[,<group>]* (dict, yang, yin, xpath, diff)\n\n" |
| 62 | #endif |
| 63 | " -p PATH, --path=PATH Search path for schema (YANG/YIN) modules. The option can be used multiple times.\n" |
| 64 | " Current working directory and path of the module being added is used implicitly.\n\n" |
| 65 | " -D, --disable-searchdir\n" |
| 66 | " Do not implicitly search in CWD for schema modules. If specified a second time,\n" |
| 67 | " do not even search the module directory (all modules must be explicitly specified).\n\n" |
| 68 | " -s, --strict Strict data parsing (do not skip unknown data),\n" |
| 69 | " has no effect for schemas.\n\n" |
| 70 | " -m, --merge Merge input data files into a single tree and validate at once,\n" |
| 71 | " has no effect for the auto, rpc, rpcreply and notif TYPEs.\n\n" |
| 72 | " -f FORMAT, --format=FORMAT\n" |
| 73 | " Convert to FORMAT. Supported formats: \n" |
| 74 | " yang, yin, tree and jsons (JSON) for schemas,\n" |
| 75 | " xml, json for data.\n" |
| 76 | " -a, --auto Modify the xml output by adding envelopes for autodetection.\n\n" |
| 77 | " -i, --allimplemented Make all the imported modules implemented.\n\n" |
| 78 | " -l, --list Print info about the loaded schemas in ietf-yang-library format,\n" |
| 79 | " the -f option applies here to specify data encoding.\n" |
| 80 | " (i - imported module, I - implemented module)\n\n" |
| 81 | " -o OUTFILE, --output=OUTFILE\n" |
| 82 | " Write the output to OUTFILE instead of stdout.\n\n" |
| 83 | " -F FEATURES, --features=FEATURES\n" |
| 84 | " Features to support, default all.\n" |
| 85 | " <modname>:[<feature>,]*\n\n" |
| 86 | " -d MODE, --default=MODE\n" |
| 87 | " Print data with default values, according to the MODE\n" |
| 88 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 89 | " must be loaded):\n" |
| 90 | " all - Add missing default nodes.\n" |
| 91 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 92 | " nodes with the attribute.\n" |
| 93 | " trim - Remove all nodes with a default value.\n" |
| 94 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n" |
| 95 | " -t TYPE, --type=TYPE\n" |
| 96 | " Specify data tree type in the input data file:\n" |
| 97 | " auto - Resolve data type (one of the following) automatically\n" |
| 98 | " (as pyang does) - applicable only on XML input data.\n" |
| 99 | " data - Complete datastore with status data (default type).\n" |
| 100 | " config - Configuration datastore (without status data).\n" |
| 101 | " get - Result of the NETCONF <get> operation.\n" |
| 102 | " getconfig - Result of the NETCONF <get-config> operation.\n" |
| 103 | " edit - Content of the NETCONF <edit-config> operation.\n" |
| 104 | " rpc - Content of the NETCONF <rpc> message, defined as YANG's rpc input statement.\n" |
| 105 | " rpcreply - Reply to the RPC. The input data <file>s are expected in pairs - each RPC reply\n" |
| 106 | " input data <file> must be followed by the origin RPC input data <file> for the reply.\n" |
| 107 | " The same rule of pairing applies also in case of 'auto' TYPE and input data file\n" |
| 108 | " containing RPC reply.\n" |
| 109 | " notif - Notification instance (content of the <notification> element without <eventTime>.\n\n" |
| 110 | " -O FILE, --operational=FILE\n" |
| 111 | " - Optional parameter for 'rpc', 'rpcreply' and 'notif' TYPEs, the FILE contains running\n" |
| 112 | " configuration datastore and state data (operational datastore) referenced from\n" |
| 113 | " the RPC/Notification. The same data apply to all input data <file>s. Note that\n" |
| 114 | " the file is validated as 'data' TYPE. Special value '!' can be used as FILE argument\n" |
| 115 | " to ignore the external references.\n\n" |
| 116 | " -y YANGLIB_PATH - Path to a yang-library data describing the initial context.\n\n" |
| 117 | "Tree output specific options:\n" |
| 118 | " --tree-help - Print help on tree symbols and exit.\n" |
| 119 | " --tree-print-groupings\n" |
| 120 | " Print top-level groupings in a separate section.\n" |
| 121 | " --tree-print-uses - Print uses nodes instead the resolved grouping nodes.\n" |
| 122 | " --tree-no-leafref-target\n" |
| 123 | " Do not print target nodes of leafrefs.\n" |
| 124 | " --tree-path=SCHEMA_PATH\n" |
| 125 | " Print only the specified subtree.\n" |
| 126 | " --tree-line-length=LINE_LENGTH\n" |
| 127 | " Wrap lines if longer than the specified length (it is not a strict limit, longer lines\n" |
| 128 | " can often appear).\n" |
| 129 | "\n"); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | tree_help(void) |
| 134 | { |
| 135 | fprintf(stdout, "Each node is printed as:\n\n"); |
| 136 | fprintf(stdout, "<status> <flags> <name> <opts> <type> <if-features>\n\n" |
| 137 | " <status> is one of:\n" |
| 138 | " + for current\n" |
| 139 | " x for deprecated\n" |
| 140 | " o for obsolete\n\n" |
| 141 | " <flags> is one of:\n" |
| 142 | " rw for configuration data\n" |
| 143 | " ro for status data\n" |
| 144 | " -x for RPCs\n" |
| 145 | " -n for Notification\n\n" |
| 146 | " <name> is the name of the node\n" |
| 147 | " (<name>) means that the node is a choice node\n" |
| 148 | " :(<name>) means that the node is a case node\n\n" |
| 149 | " if the node is augmented into the tree from another module,\n" |
| 150 | " it is printed with the module name as <module-name>:<name>.\n\n" |
| 151 | " <opts> is one of:\n" |
| 152 | " ? for an optional leaf or choice\n" |
| 153 | " ! for a presence container\n" |
| 154 | " * for a leaf-list or list\n" |
| 155 | " [<keys>] for a list's keys\n\n" |
| 156 | " <type> is the name of the type for leafs and leaf-lists\n" |
| 157 | " If there is a default value defined, it is printed within\n" |
| 158 | " angle brackets <default-value>.\n" |
| 159 | " If the type is a leafref, the type is printed as -> TARGET`\n\n" |
| 160 | " <if-features> is the list of features this node depends on,\n" |
| 161 | " printed within curly brackets and a question mark {...}?\n\n"); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | version(void) |
| 166 | { |
| 167 | fprintf(stdout, "yanglint %s\n", PROJECT_VERSION); |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 172 | { |
| 173 | char *levstr; |
| 174 | |
| 175 | if (level <= verbose) { |
| 176 | switch(level) { |
| 177 | case LY_LLERR: |
| 178 | levstr = "err :"; |
| 179 | break; |
| 180 | case LY_LLWRN: |
| 181 | levstr = "warn:"; |
| 182 | break; |
| 183 | case LY_LLVRB: |
| 184 | levstr = "verb:"; |
| 185 | break; |
| 186 | default: |
| 187 | levstr = "dbg :"; |
| 188 | break; |
| 189 | } |
| 190 | if (path) { |
| 191 | fprintf(stderr, "%s %s (%s)\n", levstr, msg, path); |
| 192 | } else { |
| 193 | fprintf(stderr, "%s %s\n", levstr, msg); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * return: |
| 200 | * 0 - error |
| 201 | * 1 - schema format |
| 202 | * 2 - data format |
| 203 | */ |
| 204 | static int |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 205 | get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 206 | { |
| 207 | char *ptr; |
| 208 | LYS_INFORMAT informat_s; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 209 | LYD_FORMAT informat_d; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 210 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 211 | /* get the file format */ |
| 212 | if ((ptr = strrchr(filename, '.')) != NULL) { |
| 213 | ++ptr; |
| 214 | if (!strcmp(ptr, "yang")) { |
| 215 | informat_s = LYS_IN_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 216 | informat_d = 0; |
| 217 | } else if (!strcmp(ptr, "yin")) { |
| 218 | informat_s = LYS_IN_YIN; |
| 219 | informat_d = 0; |
| 220 | } else if (!strcmp(ptr, "xml")) { |
| 221 | informat_s = 0; |
| 222 | informat_d = LYD_XML; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 223 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 224 | } else if (!strcmp(ptr, "json")) { |
| 225 | informat_s = 0; |
| 226 | informat_d = LYD_JSON; |
| 227 | #endif |
| 228 | } else { |
| 229 | fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr); |
| 230 | return 0; |
| 231 | } |
| 232 | } else { |
| 233 | fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename); |
| 234 | return 0; |
| 235 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 236 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 237 | if (data) { |
| 238 | (*data) = informat_d; |
| 239 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 240 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 241 | if (schema) { |
| 242 | (*schema) = informat_s; |
| 243 | } |
| 244 | |
| 245 | if (informat_s) { |
| 246 | return 1; |
| 247 | } else { |
| 248 | return 2; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | int |
| 253 | main_ni(int argc, char* argv[]) |
| 254 | { |
| 255 | int ret = EXIT_FAILURE; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 256 | int opt, opt_index = 0, i, featsize = 0, compiled = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 257 | struct option options[] = { |
| 258 | #if 0 |
| 259 | {"auto", no_argument, NULL, 'a'}, |
| 260 | {"default", required_argument, NULL, 'd'}, |
| 261 | #endif |
| 262 | {"format", required_argument, NULL, 'f'}, |
| 263 | {"features", required_argument, NULL, 'F'}, |
| 264 | #if 0 |
| 265 | {"tree-print-groupings", no_argument, NULL, 'g'}, |
| 266 | {"tree-print-uses", no_argument, NULL, 'u'}, |
| 267 | {"tree-no-leafref-target", no_argument, NULL, 'n'}, |
| 268 | {"tree-path", required_argument, NULL, 'P'}, |
| 269 | {"tree-line-length", required_argument, NULL, 'L'}, |
| 270 | #endif |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 271 | {"compiled", no_argument, NULL, 'c'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 272 | {"help", no_argument, NULL, 'h'}, |
| 273 | #if 0 |
| 274 | {"tree-help", no_argument, NULL, 'H'}, |
| 275 | #endif |
| 276 | {"allimplemented", no_argument, NULL, 'i'}, |
| 277 | {"disable-cwd-search", no_argument, NULL, 'D'}, |
| 278 | {"list", no_argument, NULL, 'l'}, |
| 279 | #if 0 |
| 280 | {"merge", no_argument, NULL, 'm'}, |
| 281 | #endif |
| 282 | {"output", required_argument, NULL, 'o'}, |
| 283 | {"path", required_argument, NULL, 'p'}, |
| 284 | #if 0 |
| 285 | {"running", required_argument, NULL, 'r'}, |
| 286 | {"operational", required_argument, NULL, 'O'}, |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 287 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 288 | {"strict", no_argument, NULL, 's'}, |
| 289 | {"type", required_argument, NULL, 't'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 290 | {"version", no_argument, NULL, 'v'}, |
| 291 | {"verbose", no_argument, NULL, 'V'}, |
| 292 | #ifndef NDEBUG |
| 293 | {"debug", required_argument, NULL, 'G'}, |
| 294 | #endif |
| 295 | {NULL, required_argument, NULL, 'y'}, |
| 296 | {NULL, 0, NULL, 0} |
| 297 | }; |
| 298 | FILE *out = stdout; |
| 299 | struct ly_ctx *ctx = NULL; |
| 300 | const struct lys_module *mod; |
| 301 | LYS_OUTFORMAT outformat_s = 0; |
| 302 | LYS_INFORMAT informat_s; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 303 | LYD_FORMAT informat_d, outformat_d = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 304 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 305 | LYD_FORMAT ylformat = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 306 | #endif |
| 307 | struct ly_set *searchpaths = NULL; |
| 308 | const char *outtarget_s = NULL; |
| 309 | char **feat = NULL, *ptr, *featlist, *dir; |
| 310 | struct stat st; |
| 311 | uint32_t u; |
| 312 | int options_ctx = LY_CTX_NOYANGLIBRARY, list = 0, outoptions_s = 0, outline_length_s = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 313 | int autodetection = 0, options_parser = 0, merge = 0; |
| 314 | const char *oper_file = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 315 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 316 | const char *envelope_s = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 317 | char *ylpath = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 318 | int options_dflt = 0, envelope = 0; |
| 319 | struct lyxml_elem *iter, *elem; |
| 320 | struct *subroot, *next, *node; |
| 321 | #endif |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 322 | struct lyd_node *tree = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 323 | struct dataitem { |
| 324 | const char *filename; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 325 | struct lyd_node *tree; |
| 326 | struct dataitem *next; |
| 327 | LYD_FORMAT format; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 328 | int flags; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 329 | } *data = NULL, *data_item, *data_prev = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 330 | struct ly_set *mods = NULL; |
| 331 | void *p; |
| 332 | int index = 0; |
| 333 | |
| 334 | opterr = 0; |
| 335 | #ifndef NDEBUG |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 336 | while ((opt = getopt_long(argc, argv, "acd:f:F:gunP:L:hHiDlmo:p:r:O:st:vVG:y:", options, &opt_index)) != -1) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 337 | #else |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 338 | while ((opt = getopt_long(argc, argv, "acd:f:F:gunP:L:hHiDlmo:p:r:O:st:vVy:", options, &opt_index)) != -1) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 339 | #endif |
| 340 | { |
| 341 | switch (opt) { |
| 342 | #if 0 |
| 343 | case 'a': |
| 344 | envelope = 1; |
| 345 | break; |
| 346 | case 'd': |
| 347 | if (!strcmp(optarg, "all")) { |
| 348 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL; |
| 349 | } else if (!strcmp(optarg, "all-tagged")) { |
| 350 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG; |
| 351 | } else if (!strcmp(optarg, "trim")) { |
| 352 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM; |
| 353 | } else if (!strcmp(optarg, "implicit-tagged")) { |
| 354 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG; |
| 355 | } else { |
| 356 | fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg); |
| 357 | help(1); |
| 358 | goto cleanup; |
| 359 | } |
| 360 | break; |
| 361 | #endif |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 362 | case 'c': |
| 363 | compiled = 1; |
| 364 | break; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 365 | case 'f': |
| 366 | if (!strcasecmp(optarg, "yang")) { |
| 367 | outformat_s = LYS_OUT_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 368 | outformat_d = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 369 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 370 | } else if (!strcasecmp(optarg, "tree")) { |
| 371 | outformat_s = LYS_OUT_TREE; |
| 372 | outformat_d = 0; |
| 373 | } else if (!strcasecmp(optarg, "tree-rfc")) { |
| 374 | outformat_s = LYS_OUT_TREE; |
| 375 | outoptions_s |= LYS_OUTOPT_TREE_RFC; |
| 376 | outformat_d = 0; |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 377 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 378 | } else if (!strcasecmp(optarg, "yin")) { |
| 379 | outformat_s = LYS_OUT_YIN; |
| 380 | outformat_d = 0; |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 381 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 382 | } else if (!strcasecmp(optarg, "jsons")) { |
| 383 | outformat_s = LYS_OUT_JSON; |
| 384 | outformat_d = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 385 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 386 | } else if (!strcasecmp(optarg, "xml")) { |
| 387 | outformat_s = 0; |
| 388 | outformat_d = LYD_XML; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 389 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 390 | } else if (!strcasecmp(optarg, "json")) { |
| 391 | outformat_s = 0; |
| 392 | outformat_d = LYD_JSON; |
| 393 | #endif |
| 394 | } else { |
| 395 | fprintf(stderr, "yanglint error: unknown output format %s\n", optarg); |
| 396 | help(1); |
| 397 | goto cleanup; |
| 398 | } |
| 399 | break; |
| 400 | case 'F': |
| 401 | featsize++; |
| 402 | if (!feat) { |
| 403 | p = malloc(sizeof *feat); |
| 404 | } else { |
| 405 | p = realloc(feat, featsize * sizeof *feat); |
| 406 | } |
| 407 | if (!p) { |
| 408 | fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 409 | goto cleanup; |
| 410 | } |
| 411 | feat = p; |
| 412 | feat[featsize - 1] = strdup(optarg); |
| 413 | ptr = strchr(feat[featsize - 1], ':'); |
| 414 | if (!ptr) { |
| 415 | fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg); |
| 416 | goto cleanup; |
| 417 | } |
| 418 | *ptr = '\0'; |
| 419 | |
| 420 | break; |
| 421 | #if 0 |
| 422 | case 'g': |
| 423 | outoptions_s |= LYS_OUTOPT_TREE_GROUPING; |
| 424 | break; |
| 425 | case 'u': |
| 426 | outoptions_s |= LYS_OUTOPT_TREE_USES; |
| 427 | break; |
| 428 | case 'n': |
| 429 | outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF; |
| 430 | break; |
| 431 | case 'P': |
| 432 | outtarget_s = optarg; |
| 433 | break; |
| 434 | case 'L': |
| 435 | outline_length_s = atoi(optarg); |
| 436 | break; |
| 437 | #endif |
| 438 | case 'h': |
| 439 | help(0); |
| 440 | ret = EXIT_SUCCESS; |
| 441 | goto cleanup; |
| 442 | #if 0 |
| 443 | case 'H': |
| 444 | tree_help(); |
| 445 | ret = EXIT_SUCCESS; |
| 446 | goto cleanup; |
| 447 | #endif |
| 448 | case 'i': |
| 449 | options_ctx |= LY_CTX_ALLIMPLEMENTED; |
| 450 | break; |
| 451 | case 'D': |
| 452 | if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) { |
| 453 | fprintf(stderr, "yanglint error: -D specified too many times.\n"); |
| 454 | goto cleanup; |
| 455 | } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) { |
| 456 | options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 457 | options_ctx |= LY_CTX_DISABLE_SEARCHDIRS; |
| 458 | } else { |
| 459 | options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 460 | } |
| 461 | break; |
| 462 | case 'l': |
| 463 | list = 1; |
| 464 | break; |
| 465 | #if 0 |
| 466 | case 'm': |
| 467 | merge = 1; |
| 468 | break; |
| 469 | #endif |
| 470 | case 'o': |
| 471 | if (out != stdout) { |
| 472 | fclose(out); |
| 473 | } |
| 474 | out = fopen(optarg, "w"); |
| 475 | if (!out) { |
| 476 | fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 477 | goto cleanup; |
| 478 | } |
| 479 | break; |
| 480 | case 'p': |
| 481 | if (stat(optarg, &st) == -1) { |
| 482 | fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno)); |
| 483 | goto cleanup; |
| 484 | } |
| 485 | if (!S_ISDIR(st.st_mode)) { |
| 486 | fprintf(stderr, "yanglint error: Provided search path is not a directory.\n"); |
| 487 | goto cleanup; |
| 488 | } |
| 489 | if (!searchpaths) { |
| 490 | searchpaths = ly_set_new(); |
| 491 | } |
| 492 | ly_set_add(searchpaths, optarg, 0); |
| 493 | break; |
| 494 | #if 0 |
| 495 | case 'r': |
| 496 | case 'O': |
| 497 | if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) { |
| 498 | fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n"); |
| 499 | goto cleanup; |
| 500 | } |
| 501 | if (optarg[0] == '!') { |
| 502 | /* ignore extenral dependencies to the operational datastore */ |
| 503 | options_parser |= LYD_OPT_NOEXTDEPS; |
| 504 | } else { |
| 505 | /* external file with the operational datastore */ |
| 506 | oper_file = optarg; |
| 507 | } |
| 508 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 509 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 510 | case 's': |
| 511 | options_parser |= LYD_OPT_STRICT; |
| 512 | break; |
| 513 | case 't': |
| 514 | if (!strcmp(optarg, "auto")) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 515 | autodetection = 1; |
| 516 | } else if (!strcmp(optarg, "config")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 517 | options_parser |= LYD_OPT_CONFIG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 518 | } else if (!strcmp(optarg, "get")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 519 | options_parser |= LYD_OPT_GET; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 520 | } else if (!strcmp(optarg, "getconfig")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 521 | options_parser |= LYD_OPT_GETCONFIG; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 522 | /*} else if (!strcmp(optarg, "edit")) { |
| 523 | options_parser |= LYD_OPT_EDIT;*/ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 524 | } else if (!strcmp(optarg, "data")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 525 | /* no options */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 526 | } else { |
| 527 | fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg); |
| 528 | help(1); |
| 529 | goto cleanup; |
| 530 | } |
| 531 | break; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 532 | case 'v': |
| 533 | version(); |
| 534 | ret = EXIT_SUCCESS; |
| 535 | goto cleanup; |
| 536 | case 'V': |
| 537 | verbose++; |
| 538 | break; |
| 539 | #ifndef NDEBUG |
| 540 | case 'G': |
| 541 | u = 0; |
| 542 | ptr = optarg; |
| 543 | while (ptr[0]) { |
| 544 | if (!strncmp(ptr, "dict", 4)) { |
| 545 | u |= LY_LDGDICT; |
| 546 | ptr += 4; |
| 547 | } else if (!strncmp(ptr, "yang", 4)) { |
| 548 | u |= LY_LDGYANG; |
| 549 | ptr += 4; |
| 550 | } else if (!strncmp(ptr, "yin", 3)) { |
| 551 | u |= LY_LDGYIN; |
| 552 | ptr += 3; |
| 553 | } else if (!strncmp(ptr, "xpath", 5)) { |
| 554 | u |= LY_LDGXPATH; |
| 555 | ptr += 5; |
| 556 | } else if (!strncmp(ptr, "diff", 4)) { |
| 557 | u |= LY_LDGDIFF; |
| 558 | ptr += 4; |
| 559 | } |
| 560 | |
| 561 | if (ptr[0]) { |
| 562 | if (ptr[0] != ',') { |
| 563 | fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg); |
| 564 | goto cleanup; |
| 565 | } |
| 566 | ++ptr; |
| 567 | } |
| 568 | } |
| 569 | ly_verb_dbg(u); |
| 570 | break; |
| 571 | #endif |
| 572 | #if 0 |
| 573 | case 'y': |
| 574 | ptr = strrchr(optarg, '.'); |
| 575 | if (ptr) { |
| 576 | ptr++; |
| 577 | if (!strcmp(ptr, "xml")) { |
| 578 | ylformat = LYD_XML; |
| 579 | } else if (!strcmp(ptr, "json")) { |
| 580 | ylformat = LYD_JSON; |
| 581 | } else { |
| 582 | fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr); |
| 583 | goto cleanup; |
| 584 | } |
| 585 | } else { |
| 586 | fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n"); |
| 587 | goto cleanup; |
| 588 | } |
| 589 | ylpath = optarg; |
| 590 | break; |
| 591 | #endif |
| 592 | default: |
| 593 | help(1); |
| 594 | if (optopt) { |
| 595 | fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt); |
| 596 | } else { |
| 597 | fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]); |
| 598 | } |
| 599 | goto cleanup; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /* check options compatibility */ |
| 604 | if (!list && optind >= argc) { |
| 605 | help(1); |
| 606 | fprintf(stderr, "yanglint error: missing <file> to process\n"); |
| 607 | goto cleanup; |
| 608 | } |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 609 | if (compiled) { |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 610 | if (outformat_s != LYS_OUT_YANG) { |
| 611 | fprintf(stderr, "yanglint warning: --compiled option takes effect only in case of printing schemas in YANG format.\n"); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 612 | } else { |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 613 | outformat_s = LYS_OUT_YANG_COMPILED; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 614 | } |
| 615 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 616 | if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) { |
| 617 | /* we have multiple schemas to be printed as YIN or YANG */ |
| 618 | fprintf(stderr, "yanglint error: too many schemas to convert and store.\n"); |
| 619 | goto cleanup; |
| 620 | } |
| 621 | if (outoptions_s || outtarget_s || outline_length_s) { |
| 622 | #if 0 |
| 623 | if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) { |
| 624 | /* we have --tree-print-grouping with other output format than tree */ |
| 625 | fprintf(stderr, |
| 626 | "yanglint warning: --tree options take effect only in case of the tree output format.\n"); |
| 627 | } |
| 628 | } |
| 629 | if (merge) { |
| 630 | if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) { |
| 631 | fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n"); |
| 632 | merge = 0; |
| 633 | } else { |
| 634 | /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged |
| 635 | * and the result will be validated */ |
| 636 | options_parser |= LYD_OPT_TRUSTED; |
| 637 | } |
| 638 | #endif |
| 639 | } |
| 640 | #if 0 |
| 641 | if (!outformat_d && options_dflt) { |
| 642 | /* we have options for printing default nodes, but data output not specified */ |
| 643 | fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n"); |
| 644 | } |
| 645 | if (outformat_s && (options_parser || autodetection)) { |
| 646 | /* we have options for printing data tree, but output is schema */ |
| 647 | fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n"); |
| 648 | } |
| 649 | if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) { |
| 650 | fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n"); |
| 651 | /* ignore operational datastore file */ |
| 652 | oper_file = NULL; |
| 653 | } |
| 654 | if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) { |
| 655 | /* add option to ignore ietf-yang-library data for implicit data type */ |
| 656 | options_parser |= LYD_OPT_DATA_NO_YANGLIB; |
| 657 | } |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 658 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 659 | |
| 660 | /* set callback for printing libyang messages */ |
| 661 | ly_set_log_clb(libyang_verbclb, 1); |
| 662 | #if 0 |
| 663 | /* create libyang context */ |
| 664 | if (ylpath) { |
| 665 | ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx); |
| 666 | } else { |
| 667 | #else |
| 668 | { |
| 669 | #endif |
| 670 | ly_ctx_new(NULL, options_ctx, &ctx); |
| 671 | } |
| 672 | if (!ctx) { |
| 673 | goto cleanup; |
| 674 | } |
| 675 | |
| 676 | /* set searchpaths */ |
| 677 | if (searchpaths) { |
| 678 | for (u = 0; u < searchpaths->count; u++) { |
| 679 | ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]); |
| 680 | } |
| 681 | index = u + 1; |
| 682 | } |
| 683 | |
| 684 | /* derefered setting of verbosity in libyang after context initiation */ |
| 685 | ly_verb(verbose); |
| 686 | |
| 687 | mods = ly_set_new(); |
| 688 | |
| 689 | |
| 690 | /* divide input files */ |
| 691 | for (i = 0; i < argc - optind; i++) { |
| 692 | /* get the file format */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 693 | if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 694 | goto cleanup; |
| 695 | } |
| 696 | |
| 697 | if (informat_s) { |
| 698 | /* load/validate schema */ |
| 699 | if (verbose >= 2) { |
| 700 | fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]); |
| 701 | } |
| 702 | dir = strdup(argv[optind + i]); |
| 703 | ly_ctx_set_searchdir(ctx, ptr = dirname(dir)); |
| 704 | mod = lys_parse_path(ctx, argv[optind + i], informat_s); |
| 705 | ly_ctx_unset_searchdir(ctx, index); |
| 706 | free(dir); |
| 707 | if (!mod) { |
| 708 | goto cleanup; |
| 709 | } |
| 710 | ly_set_add(mods, (void *)mod, 0); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 711 | } else { |
| 712 | if (autodetection && informat_d != LYD_XML) { |
| 713 | /* data file content autodetection is possible only for XML input */ |
| 714 | fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n"); |
| 715 | goto cleanup; |
| 716 | } |
| 717 | |
| 718 | /* remember data filename and its format */ |
| 719 | if (!data) { |
| 720 | data = data_item = malloc(sizeof *data); |
| 721 | } else { |
| 722 | for (data_item = data; data_item->next; data_item = data_item->next); |
| 723 | data_item->next = malloc(sizeof *data_item); |
| 724 | data_item = data_item->next; |
| 725 | } |
| 726 | data_item->filename = argv[optind + i]; |
| 727 | data_item->format = informat_d; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 728 | data_item->flags = options_parser; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 729 | data_item->tree = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 730 | data_item->next = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 731 | } |
| 732 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 733 | if (outformat_d && !data && !list) { |
| 734 | fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n"); |
| 735 | goto cleanup; |
| 736 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 737 | |
| 738 | /* enable specified features, if not specified, all the module's features are enabled */ |
| 739 | u = 4; /* skip internal libyang modules */ |
| 740 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
| 741 | for (i = 0; i < featsize; i++) { |
| 742 | if (!strcmp(feat[i], mod->name)) { |
| 743 | /* parse features spec */ |
| 744 | featlist = strdup(feat[i] + strlen(feat[i]) + 1); |
| 745 | ptr = NULL; |
| 746 | while((ptr = strtok(ptr ? NULL : featlist, ","))) { |
| 747 | if (verbose >= 2) { |
| 748 | fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name); |
| 749 | } |
| 750 | if (lys_feature_enable(mod, ptr)) { |
| 751 | fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name); |
| 752 | } |
| 753 | } |
| 754 | free(featlist); |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | if (i == featsize) { |
| 759 | if (verbose >= 2) { |
| 760 | fprintf(stdout, "Enabling all features in module %s.\n", mod->name); |
| 761 | } |
| 762 | lys_feature_enable(mod, "*"); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* convert (print) to FORMAT */ |
| 767 | if (outformat_s) { |
| 768 | if (outformat_s == LYS_OUT_JSON && mods->count > 1) { |
| 769 | fputs("[", out); |
| 770 | } |
| 771 | for (u = 0; u < mods->count; u++) { |
| 772 | if (u) { |
| 773 | if (outformat_s == LYS_OUT_JSON) { |
| 774 | fputs(",\n", out); |
| 775 | } else { |
| 776 | fputs("\n", out); |
| 777 | } |
| 778 | } |
| 779 | lys_print_file(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s); |
| 780 | } |
| 781 | if (outformat_s == LYS_OUT_JSON) { |
| 782 | if (mods->count > 1) { |
| 783 | fputs("]\n", out); |
| 784 | } else if (mods->count == 1) { |
| 785 | fputs("\n", out); |
| 786 | } |
| 787 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 788 | } else if (data) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 789 | |
| 790 | /* prepare operational datastore when specified for RPC/Notification */ |
| 791 | if (oper_file) { |
| 792 | /* get the file format */ |
| 793 | if (!get_fileformat(oper_file, NULL, &informat_d)) { |
| 794 | goto cleanup; |
| 795 | } else if (!informat_d) { |
| 796 | fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n"); |
| 797 | goto cleanup; |
| 798 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 799 | tree = lyd_parse_path(ctx, oper_file, informat_d, LYD_OPT_PARSE_ONLY); |
| 800 | if (!tree) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 801 | fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n"); |
| 802 | goto cleanup; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) { |
| 807 | /* parse data file - via LYD_OPT_TRUSTED postpone validation when all data are loaded and merged */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 808 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 809 | if (autodetection) { |
| 810 | /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */ |
| 811 | options_parser &= ~LYD_OPT_DATA_NO_YANGLIB; |
| 812 | /* automatically detect data type from the data top level */ |
| 813 | data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0); |
| 814 | if (!data_item->xml) { |
| 815 | fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n"); |
| 816 | goto cleanup; |
| 817 | } |
| 818 | |
| 819 | /* NOTE: namespace is ignored to simplify usage of this feature */ |
| 820 | if (!strcmp(data_item->xml->name, "data")) { |
| 821 | if (verbose >= 2) { |
| 822 | fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename); |
| 823 | } |
| 824 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB; |
| 825 | data_item->type = LYD_OPT_DATA; |
| 826 | } else if (!strcmp(data_item->xml->name, "config")) { |
| 827 | if (verbose >= 2) { |
| 828 | fprintf(stdout, "Parsing %s as config data.\n", data_item->filename); |
| 829 | } |
| 830 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 831 | data_item->type = LYD_OPT_CONFIG; |
| 832 | } else if (!strcmp(data_item->xml->name, "get-reply")) { |
| 833 | if (verbose >= 2) { |
| 834 | fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename); |
| 835 | } |
| 836 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 837 | data_item->type = LYD_OPT_GET; |
| 838 | } else if (!strcmp(data_item->xml->name, "get-config-reply")) { |
| 839 | if (verbose >= 2) { |
| 840 | fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename); |
| 841 | } |
| 842 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 843 | data_item->type = LYD_OPT_GETCONFIG; |
| 844 | } else if (!strcmp(data_item->xml->name, "edit-config")) { |
| 845 | if (verbose >= 2) { |
| 846 | fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename); |
| 847 | } |
| 848 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 849 | data_item->type = LYD_OPT_EDIT; |
| 850 | } else if (!strcmp(data_item->xml->name, "rpc")) { |
| 851 | if (verbose >= 2) { |
| 852 | fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename); |
| 853 | } |
| 854 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 855 | data_item->type = LYD_OPT_RPC; |
| 856 | } else if (!strcmp(data_item->xml->name, "rpc-reply")) { |
| 857 | if (verbose >= 2) { |
| 858 | fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename); |
| 859 | } |
| 860 | |
| 861 | data_item->type = LYD_OPT_RPCREPLY; |
| 862 | if (!data_item->next || (data_prev && !data_prev->tree)) { |
| 863 | fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename); |
| 864 | goto cleanup; |
| 865 | } |
| 866 | |
| 867 | continue; |
| 868 | } else if (!strcmp(data_item->xml->name, "notification")) { |
| 869 | if (verbose >= 2) { |
| 870 | fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename); |
| 871 | } |
| 872 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 873 | data_item->type = LYD_OPT_NOTIF; |
| 874 | |
| 875 | /* ignore eventTime element if present */ |
| 876 | while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) { |
| 877 | lyxml_free(ctx, data_item->xml->child); |
| 878 | } |
| 879 | } else { |
| 880 | fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n", |
| 881 | data_item->xml->name); |
| 882 | goto cleanup; |
| 883 | } |
| 884 | |
| 885 | data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper); |
| 886 | if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) { |
| 887 | parse_reply: |
| 888 | /* check result of the RPC parsing, we are going to do another parsing in this step */ |
| 889 | if (ly_errno) { |
| 890 | goto cleanup; |
| 891 | } |
| 892 | |
| 893 | /* check that we really have RPC for the reply */ |
| 894 | if (data_item->type != LYD_OPT_RPC) { |
| 895 | fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename); |
| 896 | goto cleanup; |
| 897 | } |
| 898 | |
| 899 | if (data_prev->format == LYD_XML) { |
| 900 | /* ignore <ok> and <rpc-error> elements if present */ |
| 901 | u = 0; |
| 902 | LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) { |
| 903 | if (!strcmp(data_prev->xml->child->name, "ok")) { |
| 904 | if (u) { |
| 905 | /* rpc-error or ok already present */ |
| 906 | u = 0x8; /* error flag */ |
| 907 | } else { |
| 908 | u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */ |
| 909 | } |
| 910 | } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) { |
| 911 | if (u && (u & 0x1)) { |
| 912 | /* ok already present, rpc-error can be present multiple times */ |
| 913 | u = 0x8; /* error flag */ |
| 914 | } else { |
| 915 | u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */ |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | if (u == 0x8) { |
| 920 | fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename); |
| 921 | goto cleanup; |
| 922 | } else if (u & 0x4) { |
| 923 | lyxml_free(ctx, data_prev->xml->child); |
| 924 | u &= ~0x4; /* unset lyxml_free() flag */ |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* finally, parse RPC reply from the previous step */ |
| 929 | data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child, |
| 930 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper); |
| 931 | } else { /* LYD_JSON */ |
| 932 | data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format, |
| 933 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper); |
| 934 | } |
| 935 | } |
| 936 | } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) { |
| 937 | if (data_prev && !data_prev->tree) { |
| 938 | /* now we should have RPC for the preceding RPC reply */ |
| 939 | data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, |
| 940 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper); |
| 941 | data_item->type = LYD_OPT_RPC; |
| 942 | goto parse_reply; |
| 943 | } else { |
| 944 | /* now we have RPC reply which will be parsed in next step together with its RPC */ |
| 945 | if (!data_item->next) { |
| 946 | fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename); |
| 947 | goto cleanup; |
| 948 | } |
| 949 | if (data_item->format == LYD_XML) { |
| 950 | /* create rpc-reply container to unify handling with autodetection */ |
| 951 | data_item->xml = calloc(1, sizeof *data_item->xml); |
| 952 | if (!data_item->xml) { |
| 953 | fprintf(stderr, "yanglint error: Memory allocation failed failed.\n"); |
| 954 | goto cleanup; |
| 955 | } |
| 956 | data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9); |
| 957 | data_item->xml->prev = data_item->xml; |
| 958 | data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT); |
| 959 | if (data_item->xml->child) { |
| 960 | data_item->xml->child->parent = data_item->xml; |
| 961 | } |
| 962 | } |
| 963 | continue; |
| 964 | } |
| 965 | } else { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 966 | #else |
| 967 | { |
| 968 | #endif |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 969 | data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 970 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 971 | if (ly_err_first(ctx)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 972 | goto cleanup; |
| 973 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 974 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 975 | if (merge && data != data_item) { |
| 976 | if (!data->tree) { |
| 977 | data->tree = data_item->tree; |
| 978 | } else if (data_item->tree) { |
| 979 | /* merge results */ |
| 980 | if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) { |
| 981 | fprintf(stderr, "yanglint error: merging multiple data trees failed.\n"); |
| 982 | goto cleanup; |
| 983 | } |
| 984 | } |
| 985 | data_item->tree = NULL; |
| 986 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 987 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 988 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 989 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 990 | if (merge) { |
| 991 | /* validate the merged data tree, do not trust the input, invalidate all the data first */ |
| 992 | LY_TREE_FOR(data->tree, subroot) { |
| 993 | LY_TREE_DFS_BEGIN(subroot, next, node) { |
| 994 | node->validity = LYD_VAL_OK; |
| 995 | switch (node->schema->nodetype) { |
| 996 | case LYS_LEAFLIST: |
| 997 | case LYS_LEAF: |
| 998 | if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) { |
| 999 | node->validity |= LYD_VAL_LEAFREF; |
| 1000 | } |
| 1001 | break; |
| 1002 | case LYS_LIST: |
| 1003 | node->validity |= LYD_VAL_UNIQUE; |
| 1004 | /* falls through */ |
| 1005 | case LYS_CONTAINER: |
| 1006 | case LYS_NOTIF: |
| 1007 | case LYS_RPC: |
| 1008 | case LYS_ACTION: |
| 1009 | node->validity |= LYD_VAL_MAND; |
| 1010 | break; |
| 1011 | default: |
| 1012 | break; |
| 1013 | } |
| 1014 | LY_TREE_DFS_END(subroot, next, node) |
| 1015 | } |
| 1016 | } |
| 1017 | if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) { |
| 1018 | goto cleanup; |
| 1019 | } |
| 1020 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1021 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1022 | /* print only if data output format specified */ |
| 1023 | if (outformat_d) { |
| 1024 | for (data_item = data; data_item; data_item = data_item->next) { |
| 1025 | if (!merge && verbose >= 2) { |
| 1026 | fprintf(stdout, "File %s:\n", data_item->filename); |
| 1027 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1028 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1029 | if (outformat_d == LYD_XML && envelope) { |
| 1030 | switch (data_item->type) { |
| 1031 | case LYD_OPT_DATA: |
| 1032 | envelope_s = "data"; |
| 1033 | break; |
| 1034 | case LYD_OPT_CONFIG: |
| 1035 | envelope_s = "config"; |
| 1036 | break; |
| 1037 | case LYD_OPT_GET: |
| 1038 | envelope_s = "get-reply"; |
| 1039 | break; |
| 1040 | case LYD_OPT_GETCONFIG: |
| 1041 | envelope_s = "get-config-reply"; |
| 1042 | break; |
| 1043 | case LYD_OPT_EDIT: |
| 1044 | envelope_s = "edit-config"; |
| 1045 | break; |
| 1046 | case LYD_OPT_RPC: |
| 1047 | envelope_s = "rpc"; |
| 1048 | break; |
| 1049 | case LYD_OPT_RPCREPLY: |
| 1050 | envelope_s = "rpc-reply"; |
| 1051 | break; |
| 1052 | case LYD_OPT_NOTIF: |
| 1053 | envelope_s = "notification"; |
| 1054 | break; |
| 1055 | } |
| 1056 | fprintf(out, "<%s>\n", envelope_s); |
| 1057 | if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) { |
| 1058 | /* action */ |
| 1059 | fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n"); |
| 1060 | } |
| 1061 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1062 | #endif |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 1063 | lyd_print_file(out, data_item->tree, outformat_d, LYDP_WITHSIBLINGS | LYDP_FORMAT /* TODO defaults | options_dflt */); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1064 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1065 | if (envelope_s) { |
| 1066 | if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) { |
| 1067 | fprintf(out, "</action>\n"); |
| 1068 | } |
| 1069 | fprintf(out, "</%s>\n", envelope_s); |
| 1070 | } |
| 1071 | if (merge) { |
| 1072 | /* stop after first item */ |
| 1073 | break; |
| 1074 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1075 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1076 | } |
| 1077 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1078 | } |
| 1079 | #if 0 |
| 1080 | if (list) { |
| 1081 | print_list(out, ctx, outformat_d); |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
| 1085 | ret = EXIT_SUCCESS; |
| 1086 | |
| 1087 | cleanup: |
| 1088 | if (out && out != stdout) { |
| 1089 | fclose(out); |
| 1090 | } |
| 1091 | ly_set_free(mods, NULL); |
| 1092 | ly_set_free(searchpaths, NULL); |
| 1093 | for (i = 0; i < featsize; i++) { |
| 1094 | free(feat[i]); |
| 1095 | } |
| 1096 | free(feat); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1097 | for (; data; data = data_item) { |
| 1098 | data_item = data->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1099 | lyd_free_all(data->tree); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1100 | free(data); |
| 1101 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1102 | ly_ctx_destroy(ctx, NULL); |
| 1103 | |
| 1104 | return ret; |
| 1105 | } |