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"); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 42 | fprintf(stdout, " yanglint [options] [-f { yang | yin | tree | tree-rfc | info}] <file>...\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 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" |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 74 | " yang, yin, tree and info for schemas,\n" |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 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" |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 128 | " can often appear).\n\n" |
| 129 | "Info output specific options:\n" |
| 130 | " -P INFOPATH, --info-path=INFOPATH\n" |
| 131 | " - Schema path with full module names used as node's prefixes, the path identify the root\n" |
| 132 | " node of the subtree to print information about.\n" |
| 133 | " --single-node - Print information about a single node instead of a subtree." |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 134 | "\n"); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | tree_help(void) |
| 139 | { |
| 140 | fprintf(stdout, "Each node is printed as:\n\n"); |
| 141 | fprintf(stdout, "<status> <flags> <name> <opts> <type> <if-features>\n\n" |
| 142 | " <status> is one of:\n" |
| 143 | " + for current\n" |
| 144 | " x for deprecated\n" |
| 145 | " o for obsolete\n\n" |
| 146 | " <flags> is one of:\n" |
| 147 | " rw for configuration data\n" |
| 148 | " ro for status data\n" |
| 149 | " -x for RPCs\n" |
| 150 | " -n for Notification\n\n" |
| 151 | " <name> is the name of the node\n" |
| 152 | " (<name>) means that the node is a choice node\n" |
| 153 | " :(<name>) means that the node is a case node\n\n" |
| 154 | " if the node is augmented into the tree from another module,\n" |
| 155 | " it is printed with the module name as <module-name>:<name>.\n\n" |
| 156 | " <opts> is one of:\n" |
| 157 | " ? for an optional leaf or choice\n" |
| 158 | " ! for a presence container\n" |
| 159 | " * for a leaf-list or list\n" |
| 160 | " [<keys>] for a list's keys\n\n" |
| 161 | " <type> is the name of the type for leafs and leaf-lists\n" |
| 162 | " If there is a default value defined, it is printed within\n" |
| 163 | " angle brackets <default-value>.\n" |
| 164 | " If the type is a leafref, the type is printed as -> TARGET`\n\n" |
| 165 | " <if-features> is the list of features this node depends on,\n" |
| 166 | " printed within curly brackets and a question mark {...}?\n\n"); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | version(void) |
| 171 | { |
| 172 | fprintf(stdout, "yanglint %s\n", PROJECT_VERSION); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 177 | { |
| 178 | char *levstr; |
| 179 | |
| 180 | if (level <= verbose) { |
| 181 | switch(level) { |
| 182 | case LY_LLERR: |
| 183 | levstr = "err :"; |
| 184 | break; |
| 185 | case LY_LLWRN: |
| 186 | levstr = "warn:"; |
| 187 | break; |
| 188 | case LY_LLVRB: |
| 189 | levstr = "verb:"; |
| 190 | break; |
| 191 | default: |
| 192 | levstr = "dbg :"; |
| 193 | break; |
| 194 | } |
| 195 | if (path) { |
| 196 | fprintf(stderr, "%s %s (%s)\n", levstr, msg, path); |
| 197 | } else { |
| 198 | fprintf(stderr, "%s %s\n", levstr, msg); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * return: |
| 205 | * 0 - error |
| 206 | * 1 - schema format |
| 207 | * 2 - data format |
| 208 | */ |
| 209 | static int |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 210 | get_fileformat(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 211 | { |
| 212 | char *ptr; |
| 213 | LYS_INFORMAT informat_s; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 214 | LYD_FORMAT informat_d; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 215 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 216 | /* get the file format */ |
| 217 | if ((ptr = strrchr(filename, '.')) != NULL) { |
| 218 | ++ptr; |
| 219 | if (!strcmp(ptr, "yang")) { |
| 220 | informat_s = LYS_IN_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 221 | informat_d = 0; |
| 222 | } else if (!strcmp(ptr, "yin")) { |
| 223 | informat_s = LYS_IN_YIN; |
| 224 | informat_d = 0; |
| 225 | } else if (!strcmp(ptr, "xml")) { |
| 226 | informat_s = 0; |
| 227 | informat_d = LYD_XML; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 228 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 229 | } else if (!strcmp(ptr, "json")) { |
| 230 | informat_s = 0; |
| 231 | informat_d = LYD_JSON; |
| 232 | #endif |
| 233 | } else { |
| 234 | fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr); |
| 235 | return 0; |
| 236 | } |
| 237 | } else { |
| 238 | fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename); |
| 239 | return 0; |
| 240 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 241 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 242 | if (data) { |
| 243 | (*data) = informat_d; |
| 244 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 245 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 246 | if (schema) { |
| 247 | (*schema) = informat_s; |
| 248 | } |
| 249 | |
| 250 | if (informat_s) { |
| 251 | return 1; |
| 252 | } else { |
| 253 | return 2; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | int |
| 258 | main_ni(int argc, char* argv[]) |
| 259 | { |
| 260 | int ret = EXIT_FAILURE; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 261 | int opt, opt_index = 0, i, featsize = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 262 | struct option options[] = { |
| 263 | #if 0 |
| 264 | {"auto", no_argument, NULL, 'a'}, |
| 265 | {"default", required_argument, NULL, 'd'}, |
| 266 | #endif |
| 267 | {"format", required_argument, NULL, 'f'}, |
| 268 | {"features", required_argument, NULL, 'F'}, |
| 269 | #if 0 |
| 270 | {"tree-print-groupings", no_argument, NULL, 'g'}, |
| 271 | {"tree-print-uses", no_argument, NULL, 'u'}, |
| 272 | {"tree-no-leafref-target", no_argument, NULL, 'n'}, |
| 273 | {"tree-path", required_argument, NULL, 'P'}, |
| 274 | {"tree-line-length", required_argument, NULL, 'L'}, |
| 275 | #endif |
| 276 | {"help", no_argument, NULL, 'h'}, |
| 277 | #if 0 |
| 278 | {"tree-help", no_argument, NULL, 'H'}, |
| 279 | #endif |
| 280 | {"allimplemented", no_argument, NULL, 'i'}, |
| 281 | {"disable-cwd-search", no_argument, NULL, 'D'}, |
| 282 | {"list", no_argument, NULL, 'l'}, |
| 283 | #if 0 |
| 284 | {"merge", no_argument, NULL, 'm'}, |
| 285 | #endif |
| 286 | {"output", required_argument, NULL, 'o'}, |
| 287 | {"path", required_argument, NULL, 'p'}, |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 288 | {"info-path", required_argument, NULL, 'P'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 289 | #if 0 |
| 290 | {"running", required_argument, NULL, 'r'}, |
| 291 | {"operational", required_argument, NULL, 'O'}, |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 292 | #endif |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 293 | {"single-node", no_argument, NULL, 'q'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 294 | {"strict", no_argument, NULL, 's'}, |
| 295 | {"type", required_argument, NULL, 't'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 296 | {"version", no_argument, NULL, 'v'}, |
| 297 | {"verbose", no_argument, NULL, 'V'}, |
| 298 | #ifndef NDEBUG |
| 299 | {"debug", required_argument, NULL, 'G'}, |
| 300 | #endif |
| 301 | {NULL, required_argument, NULL, 'y'}, |
| 302 | {NULL, 0, NULL, 0} |
| 303 | }; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 304 | struct lyp_out *out = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 305 | struct ly_ctx *ctx = NULL; |
| 306 | const struct lys_module *mod; |
| 307 | LYS_OUTFORMAT outformat_s = 0; |
| 308 | LYS_INFORMAT informat_s; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 309 | LYD_FORMAT informat_d, outformat_d = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 310 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 311 | LYD_FORMAT ylformat = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 312 | #endif |
| 313 | struct ly_set *searchpaths = NULL; |
| 314 | const char *outtarget_s = NULL; |
| 315 | char **feat = NULL, *ptr, *featlist, *dir; |
| 316 | struct stat st; |
| 317 | uint32_t u; |
| 318 | 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] | 319 | int autodetection = 0, options_parser = 0, merge = 0; |
| 320 | const char *oper_file = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 321 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 322 | const char *envelope_s = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 323 | char *ylpath = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 324 | int options_dflt = 0, envelope = 0; |
| 325 | struct lyxml_elem *iter, *elem; |
| 326 | struct *subroot, *next, *node; |
| 327 | #endif |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 328 | struct lyd_node *tree = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 329 | struct dataitem { |
| 330 | const char *filename; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 331 | struct lyd_node *tree; |
| 332 | struct dataitem *next; |
| 333 | LYD_FORMAT format; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 334 | int flags; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 335 | } *data = NULL, *data_item, *data_prev = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 336 | struct ly_set *mods = NULL; |
| 337 | void *p; |
| 338 | int index = 0; |
| 339 | |
| 340 | opterr = 0; |
| 341 | #ifndef NDEBUG |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 342 | 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] | 343 | #else |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 344 | 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] | 345 | #endif |
| 346 | { |
| 347 | switch (opt) { |
| 348 | #if 0 |
| 349 | case 'a': |
| 350 | envelope = 1; |
| 351 | break; |
| 352 | case 'd': |
| 353 | if (!strcmp(optarg, "all")) { |
| 354 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL; |
| 355 | } else if (!strcmp(optarg, "all-tagged")) { |
| 356 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG; |
| 357 | } else if (!strcmp(optarg, "trim")) { |
| 358 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM; |
| 359 | } else if (!strcmp(optarg, "implicit-tagged")) { |
| 360 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG; |
| 361 | } else { |
| 362 | fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg); |
| 363 | help(1); |
| 364 | goto cleanup; |
| 365 | } |
| 366 | break; |
| 367 | #endif |
| 368 | case 'f': |
| 369 | if (!strcasecmp(optarg, "yang")) { |
| 370 | outformat_s = LYS_OUT_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 371 | outformat_d = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 372 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 373 | } else if (!strcasecmp(optarg, "tree")) { |
| 374 | outformat_s = LYS_OUT_TREE; |
| 375 | outformat_d = 0; |
| 376 | } else if (!strcasecmp(optarg, "tree-rfc")) { |
| 377 | outformat_s = LYS_OUT_TREE; |
| 378 | outoptions_s |= LYS_OUTOPT_TREE_RFC; |
| 379 | outformat_d = 0; |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 380 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 381 | } else if (!strcasecmp(optarg, "yin")) { |
| 382 | outformat_s = LYS_OUT_YIN; |
| 383 | outformat_d = 0; |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 384 | } else if (!strcasecmp(optarg, "info")) { |
| 385 | outformat_s = LYS_OUT_YANG_COMPILED; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 386 | outformat_d = 0; |
| 387 | } else if (!strcasecmp(optarg, "xml")) { |
| 388 | outformat_s = 0; |
| 389 | outformat_d = LYD_XML; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 390 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 391 | } else if (!strcasecmp(optarg, "json")) { |
| 392 | outformat_s = 0; |
| 393 | outformat_d = LYD_JSON; |
| 394 | #endif |
| 395 | } else { |
| 396 | fprintf(stderr, "yanglint error: unknown output format %s\n", optarg); |
| 397 | help(1); |
| 398 | goto cleanup; |
| 399 | } |
| 400 | break; |
| 401 | case 'F': |
| 402 | featsize++; |
| 403 | if (!feat) { |
| 404 | p = malloc(sizeof *feat); |
| 405 | } else { |
| 406 | p = realloc(feat, featsize * sizeof *feat); |
| 407 | } |
| 408 | if (!p) { |
| 409 | fprintf(stderr, "yanglint error: Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
| 410 | goto cleanup; |
| 411 | } |
| 412 | feat = p; |
| 413 | feat[featsize - 1] = strdup(optarg); |
| 414 | ptr = strchr(feat[featsize - 1], ':'); |
| 415 | if (!ptr) { |
| 416 | fprintf(stderr, "yanglint error: Invalid format of the features specification (%s)", optarg); |
| 417 | goto cleanup; |
| 418 | } |
| 419 | *ptr = '\0'; |
| 420 | |
| 421 | break; |
| 422 | #if 0 |
| 423 | case 'g': |
| 424 | outoptions_s |= LYS_OUTOPT_TREE_GROUPING; |
| 425 | break; |
| 426 | case 'u': |
| 427 | outoptions_s |= LYS_OUTOPT_TREE_USES; |
| 428 | break; |
| 429 | case 'n': |
| 430 | outoptions_s |= LYS_OUTOPT_TREE_NO_LEAFREF; |
| 431 | break; |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 432 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 433 | case 'P': |
| 434 | outtarget_s = optarg; |
| 435 | break; |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 436 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 437 | case 'L': |
| 438 | outline_length_s = atoi(optarg); |
| 439 | break; |
| 440 | #endif |
| 441 | case 'h': |
| 442 | help(0); |
| 443 | ret = EXIT_SUCCESS; |
| 444 | goto cleanup; |
| 445 | #if 0 |
| 446 | case 'H': |
| 447 | tree_help(); |
| 448 | ret = EXIT_SUCCESS; |
| 449 | goto cleanup; |
| 450 | #endif |
| 451 | case 'i': |
| 452 | options_ctx |= LY_CTX_ALLIMPLEMENTED; |
| 453 | break; |
| 454 | case 'D': |
| 455 | if (options_ctx & LY_CTX_DISABLE_SEARCHDIRS) { |
| 456 | fprintf(stderr, "yanglint error: -D specified too many times.\n"); |
| 457 | goto cleanup; |
| 458 | } else if (options_ctx & LY_CTX_DISABLE_SEARCHDIR_CWD) { |
| 459 | options_ctx &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 460 | options_ctx |= LY_CTX_DISABLE_SEARCHDIRS; |
| 461 | } else { |
| 462 | options_ctx |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 463 | } |
| 464 | break; |
| 465 | case 'l': |
| 466 | list = 1; |
| 467 | break; |
| 468 | #if 0 |
| 469 | case 'm': |
| 470 | merge = 1; |
| 471 | break; |
| 472 | #endif |
| 473 | case 'o': |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 474 | if (out) { |
| 475 | if (lyp_filepath(out, optarg) != NULL) { |
| 476 | fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 477 | goto cleanup; |
| 478 | } |
| 479 | } else { |
| 480 | out = lyp_new_filepath(optarg); |
| 481 | if (!out) { |
| 482 | fprintf(stderr, "yanglint error: unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 483 | goto cleanup; |
| 484 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 485 | } |
| 486 | break; |
| 487 | case 'p': |
| 488 | if (stat(optarg, &st) == -1) { |
| 489 | fprintf(stderr, "yanglint error: Unable to use search path (%s) - %s.\n", optarg, strerror(errno)); |
| 490 | goto cleanup; |
| 491 | } |
| 492 | if (!S_ISDIR(st.st_mode)) { |
| 493 | fprintf(stderr, "yanglint error: Provided search path is not a directory.\n"); |
| 494 | goto cleanup; |
| 495 | } |
| 496 | if (!searchpaths) { |
| 497 | searchpaths = ly_set_new(); |
| 498 | } |
| 499 | ly_set_add(searchpaths, optarg, 0); |
| 500 | break; |
| 501 | #if 0 |
| 502 | case 'r': |
| 503 | case 'O': |
| 504 | if (oper_file || (options_parser & LYD_OPT_NOEXTDEPS)) { |
| 505 | fprintf(stderr, "yanglint error: The operational datastore (-O) cannot be set multiple times.\n"); |
| 506 | goto cleanup; |
| 507 | } |
| 508 | if (optarg[0] == '!') { |
| 509 | /* ignore extenral dependencies to the operational datastore */ |
| 510 | options_parser |= LYD_OPT_NOEXTDEPS; |
| 511 | } else { |
| 512 | /* external file with the operational datastore */ |
| 513 | oper_file = optarg; |
| 514 | } |
| 515 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 516 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 517 | case 's': |
| 518 | options_parser |= LYD_OPT_STRICT; |
| 519 | break; |
| 520 | case 't': |
| 521 | if (!strcmp(optarg, "auto")) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 522 | autodetection = 1; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 523 | /*} else if (!strcmp(optarg, "config")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 524 | options_parser |= LYD_OPT_CONFIG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 525 | } else if (!strcmp(optarg, "get")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 526 | options_parser |= LYD_OPT_GET; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 527 | } else if (!strcmp(optarg, "getconfig")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 528 | options_parser |= LYD_OPT_GETCONFIG; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 529 | } else if (!strcmp(optarg, "edit")) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 530 | options_parser |= LYD_OPT_EDIT;*/ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 531 | } else if (!strcmp(optarg, "data")) { |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 532 | /* no options */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 533 | } else { |
| 534 | fprintf(stderr, "yanglint error: unknown data tree type %s\n", optarg); |
| 535 | help(1); |
| 536 | goto cleanup; |
| 537 | } |
| 538 | break; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 539 | case 'v': |
| 540 | version(); |
| 541 | ret = EXIT_SUCCESS; |
| 542 | goto cleanup; |
| 543 | case 'V': |
| 544 | verbose++; |
| 545 | break; |
| 546 | #ifndef NDEBUG |
| 547 | case 'G': |
| 548 | u = 0; |
| 549 | ptr = optarg; |
| 550 | while (ptr[0]) { |
| 551 | if (!strncmp(ptr, "dict", 4)) { |
| 552 | u |= LY_LDGDICT; |
| 553 | ptr += 4; |
| 554 | } else if (!strncmp(ptr, "yang", 4)) { |
| 555 | u |= LY_LDGYANG; |
| 556 | ptr += 4; |
| 557 | } else if (!strncmp(ptr, "yin", 3)) { |
| 558 | u |= LY_LDGYIN; |
| 559 | ptr += 3; |
| 560 | } else if (!strncmp(ptr, "xpath", 5)) { |
| 561 | u |= LY_LDGXPATH; |
| 562 | ptr += 5; |
| 563 | } else if (!strncmp(ptr, "diff", 4)) { |
| 564 | u |= LY_LDGDIFF; |
| 565 | ptr += 4; |
| 566 | } |
| 567 | |
| 568 | if (ptr[0]) { |
| 569 | if (ptr[0] != ',') { |
| 570 | fprintf(stderr, "yanglint error: unknown debug group string \"%s\"\n", optarg); |
| 571 | goto cleanup; |
| 572 | } |
| 573 | ++ptr; |
| 574 | } |
| 575 | } |
| 576 | ly_verb_dbg(u); |
| 577 | break; |
| 578 | #endif |
| 579 | #if 0 |
| 580 | case 'y': |
| 581 | ptr = strrchr(optarg, '.'); |
| 582 | if (ptr) { |
| 583 | ptr++; |
| 584 | if (!strcmp(ptr, "xml")) { |
| 585 | ylformat = LYD_XML; |
| 586 | } else if (!strcmp(ptr, "json")) { |
| 587 | ylformat = LYD_JSON; |
| 588 | } else { |
| 589 | fprintf(stderr, "yanglint error: yang-library file in an unknown format \"%s\".\n", ptr); |
| 590 | goto cleanup; |
| 591 | } |
| 592 | } else { |
| 593 | fprintf(stderr, "yanglint error: yang-library file in an unknown format.\n"); |
| 594 | goto cleanup; |
| 595 | } |
| 596 | ylpath = optarg; |
| 597 | break; |
| 598 | #endif |
| 599 | default: |
| 600 | help(1); |
| 601 | if (optopt) { |
| 602 | fprintf(stderr, "yanglint error: invalid option: -%c\n", optopt); |
| 603 | } else { |
| 604 | fprintf(stderr, "yanglint error: invalid option: %s\n", argv[optind - 1]); |
| 605 | } |
| 606 | goto cleanup; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /* check options compatibility */ |
| 611 | if (!list && optind >= argc) { |
| 612 | help(1); |
| 613 | fprintf(stderr, "yanglint error: missing <file> to process\n"); |
| 614 | goto cleanup; |
| 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) { |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 768 | if (outtarget_s) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 769 | const struct lysc_node *node = lys_find_node(ctx, NULL, outtarget_s); |
| 770 | if (node) { |
| 771 | lys_print_node(out, node, outformat_s, outline_length_s, outoptions_s); |
| 772 | } else { |
| 773 | fprintf(stderr, "yanglint error: The requested schema node \"%s\" does not exists.\n", outtarget_s); |
| 774 | } |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 775 | } else { |
| 776 | for (u = 0; u < mods->count; u++) { |
| 777 | if (u) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 778 | lyp_print(out, "\n"); |
Radek Krejci | e1f6d5a | 2019-11-17 14:03:04 +0800 | [diff] [blame] | 779 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 780 | lys_print(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 781 | } |
| 782 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 783 | } else if (data) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 784 | |
| 785 | /* prepare operational datastore when specified for RPC/Notification */ |
| 786 | if (oper_file) { |
| 787 | /* get the file format */ |
| 788 | if (!get_fileformat(oper_file, NULL, &informat_d)) { |
| 789 | goto cleanup; |
| 790 | } else if (!informat_d) { |
| 791 | fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n"); |
| 792 | goto cleanup; |
| 793 | } |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 794 | tree = lyd_parse_path(ctx, oper_file, informat_d, LYD_OPT_PARSE_ONLY); |
| 795 | if (!tree) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 796 | fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n"); |
| 797 | goto cleanup; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) { |
| 802 | /* 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] | 803 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 804 | if (autodetection) { |
| 805 | /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */ |
| 806 | options_parser &= ~LYD_OPT_DATA_NO_YANGLIB; |
| 807 | /* automatically detect data type from the data top level */ |
| 808 | data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0); |
| 809 | if (!data_item->xml) { |
| 810 | fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n"); |
| 811 | goto cleanup; |
| 812 | } |
| 813 | |
| 814 | /* NOTE: namespace is ignored to simplify usage of this feature */ |
| 815 | if (!strcmp(data_item->xml->name, "data")) { |
| 816 | if (verbose >= 2) { |
| 817 | fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename); |
| 818 | } |
| 819 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB; |
| 820 | data_item->type = LYD_OPT_DATA; |
| 821 | } else if (!strcmp(data_item->xml->name, "config")) { |
| 822 | if (verbose >= 2) { |
| 823 | fprintf(stdout, "Parsing %s as config data.\n", data_item->filename); |
| 824 | } |
| 825 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 826 | data_item->type = LYD_OPT_CONFIG; |
| 827 | } else if (!strcmp(data_item->xml->name, "get-reply")) { |
| 828 | if (verbose >= 2) { |
| 829 | fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename); |
| 830 | } |
| 831 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 832 | data_item->type = LYD_OPT_GET; |
| 833 | } else if (!strcmp(data_item->xml->name, "get-config-reply")) { |
| 834 | if (verbose >= 2) { |
| 835 | fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename); |
| 836 | } |
| 837 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 838 | data_item->type = LYD_OPT_GETCONFIG; |
| 839 | } else if (!strcmp(data_item->xml->name, "edit-config")) { |
| 840 | if (verbose >= 2) { |
| 841 | fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename); |
| 842 | } |
| 843 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 844 | data_item->type = LYD_OPT_EDIT; |
| 845 | } else if (!strcmp(data_item->xml->name, "rpc")) { |
| 846 | if (verbose >= 2) { |
| 847 | fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename); |
| 848 | } |
| 849 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 850 | data_item->type = LYD_OPT_RPC; |
| 851 | } else if (!strcmp(data_item->xml->name, "rpc-reply")) { |
| 852 | if (verbose >= 2) { |
| 853 | fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename); |
| 854 | } |
| 855 | |
| 856 | data_item->type = LYD_OPT_RPCREPLY; |
| 857 | if (!data_item->next || (data_prev && !data_prev->tree)) { |
| 858 | fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename); |
| 859 | goto cleanup; |
| 860 | } |
| 861 | |
| 862 | continue; |
| 863 | } else if (!strcmp(data_item->xml->name, "notification")) { |
| 864 | if (verbose >= 2) { |
| 865 | fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename); |
| 866 | } |
| 867 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 868 | data_item->type = LYD_OPT_NOTIF; |
| 869 | |
| 870 | /* ignore eventTime element if present */ |
| 871 | while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) { |
| 872 | lyxml_free(ctx, data_item->xml->child); |
| 873 | } |
| 874 | } else { |
| 875 | fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n", |
| 876 | data_item->xml->name); |
| 877 | goto cleanup; |
| 878 | } |
| 879 | |
| 880 | data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper); |
| 881 | if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) { |
| 882 | parse_reply: |
| 883 | /* check result of the RPC parsing, we are going to do another parsing in this step */ |
| 884 | if (ly_errno) { |
| 885 | goto cleanup; |
| 886 | } |
| 887 | |
| 888 | /* check that we really have RPC for the reply */ |
| 889 | if (data_item->type != LYD_OPT_RPC) { |
| 890 | fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename); |
| 891 | goto cleanup; |
| 892 | } |
| 893 | |
| 894 | if (data_prev->format == LYD_XML) { |
| 895 | /* ignore <ok> and <rpc-error> elements if present */ |
| 896 | u = 0; |
| 897 | LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) { |
| 898 | if (!strcmp(data_prev->xml->child->name, "ok")) { |
| 899 | if (u) { |
| 900 | /* rpc-error or ok already present */ |
| 901 | u = 0x8; /* error flag */ |
| 902 | } else { |
| 903 | u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */ |
| 904 | } |
| 905 | } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) { |
| 906 | if (u && (u & 0x1)) { |
| 907 | /* ok already present, rpc-error can be present multiple times */ |
| 908 | u = 0x8; /* error flag */ |
| 909 | } else { |
| 910 | u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */ |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | if (u == 0x8) { |
| 915 | fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename); |
| 916 | goto cleanup; |
| 917 | } else if (u & 0x4) { |
| 918 | lyxml_free(ctx, data_prev->xml->child); |
| 919 | u &= ~0x4; /* unset lyxml_free() flag */ |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /* finally, parse RPC reply from the previous step */ |
| 924 | data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child, |
| 925 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper); |
| 926 | } else { /* LYD_JSON */ |
| 927 | data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format, |
| 928 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper); |
| 929 | } |
| 930 | } |
| 931 | } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) { |
| 932 | if (data_prev && !data_prev->tree) { |
| 933 | /* now we should have RPC for the preceding RPC reply */ |
| 934 | data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, |
| 935 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper); |
| 936 | data_item->type = LYD_OPT_RPC; |
| 937 | goto parse_reply; |
| 938 | } else { |
| 939 | /* now we have RPC reply which will be parsed in next step together with its RPC */ |
| 940 | if (!data_item->next) { |
| 941 | fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename); |
| 942 | goto cleanup; |
| 943 | } |
| 944 | if (data_item->format == LYD_XML) { |
| 945 | /* create rpc-reply container to unify handling with autodetection */ |
| 946 | data_item->xml = calloc(1, sizeof *data_item->xml); |
| 947 | if (!data_item->xml) { |
| 948 | fprintf(stderr, "yanglint error: Memory allocation failed failed.\n"); |
| 949 | goto cleanup; |
| 950 | } |
| 951 | data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9); |
| 952 | data_item->xml->prev = data_item->xml; |
| 953 | data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT); |
| 954 | if (data_item->xml->child) { |
| 955 | data_item->xml->child->parent = data_item->xml; |
| 956 | } |
| 957 | } |
| 958 | continue; |
| 959 | } |
| 960 | } else { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 961 | #else |
| 962 | { |
| 963 | #endif |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 964 | 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] | 965 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 966 | if (ly_err_first(ctx)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 967 | goto cleanup; |
| 968 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 969 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 970 | if (merge && data != data_item) { |
| 971 | if (!data->tree) { |
| 972 | data->tree = data_item->tree; |
| 973 | } else if (data_item->tree) { |
| 974 | /* merge results */ |
| 975 | if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) { |
| 976 | fprintf(stderr, "yanglint error: merging multiple data trees failed.\n"); |
| 977 | goto cleanup; |
| 978 | } |
| 979 | } |
| 980 | data_item->tree = NULL; |
| 981 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 982 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 983 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 984 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 985 | if (merge) { |
| 986 | /* validate the merged data tree, do not trust the input, invalidate all the data first */ |
| 987 | LY_TREE_FOR(data->tree, subroot) { |
| 988 | LY_TREE_DFS_BEGIN(subroot, next, node) { |
| 989 | node->validity = LYD_VAL_OK; |
| 990 | switch (node->schema->nodetype) { |
| 991 | case LYS_LEAFLIST: |
| 992 | case LYS_LEAF: |
| 993 | if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) { |
| 994 | node->validity |= LYD_VAL_LEAFREF; |
| 995 | } |
| 996 | break; |
| 997 | case LYS_LIST: |
| 998 | node->validity |= LYD_VAL_UNIQUE; |
| 999 | /* falls through */ |
| 1000 | case LYS_CONTAINER: |
| 1001 | case LYS_NOTIF: |
| 1002 | case LYS_RPC: |
| 1003 | case LYS_ACTION: |
| 1004 | node->validity |= LYD_VAL_MAND; |
| 1005 | break; |
| 1006 | default: |
| 1007 | break; |
| 1008 | } |
| 1009 | LY_TREE_DFS_END(subroot, next, node) |
| 1010 | } |
| 1011 | } |
| 1012 | if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) { |
| 1013 | goto cleanup; |
| 1014 | } |
| 1015 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1016 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1017 | /* print only if data output format specified */ |
| 1018 | if (outformat_d) { |
| 1019 | for (data_item = data; data_item; data_item = data_item->next) { |
| 1020 | if (!merge && verbose >= 2) { |
| 1021 | fprintf(stdout, "File %s:\n", data_item->filename); |
| 1022 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1023 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1024 | if (outformat_d == LYD_XML && envelope) { |
| 1025 | switch (data_item->type) { |
| 1026 | case LYD_OPT_DATA: |
| 1027 | envelope_s = "data"; |
| 1028 | break; |
| 1029 | case LYD_OPT_CONFIG: |
| 1030 | envelope_s = "config"; |
| 1031 | break; |
| 1032 | case LYD_OPT_GET: |
| 1033 | envelope_s = "get-reply"; |
| 1034 | break; |
| 1035 | case LYD_OPT_GETCONFIG: |
| 1036 | envelope_s = "get-config-reply"; |
| 1037 | break; |
| 1038 | case LYD_OPT_EDIT: |
| 1039 | envelope_s = "edit-config"; |
| 1040 | break; |
| 1041 | case LYD_OPT_RPC: |
| 1042 | envelope_s = "rpc"; |
| 1043 | break; |
| 1044 | case LYD_OPT_RPCREPLY: |
| 1045 | envelope_s = "rpc-reply"; |
| 1046 | break; |
| 1047 | case LYD_OPT_NOTIF: |
| 1048 | envelope_s = "notification"; |
| 1049 | break; |
| 1050 | } |
| 1051 | fprintf(out, "<%s>\n", envelope_s); |
| 1052 | if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) { |
| 1053 | /* action */ |
| 1054 | fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n"); |
| 1055 | } |
| 1056 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1057 | #endif |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 1058 | lyd_print(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] | 1059 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1060 | if (envelope_s) { |
| 1061 | if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) { |
| 1062 | fprintf(out, "</action>\n"); |
| 1063 | } |
| 1064 | fprintf(out, "</%s>\n", envelope_s); |
| 1065 | } |
| 1066 | if (merge) { |
| 1067 | /* stop after first item */ |
| 1068 | break; |
| 1069 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1070 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1071 | } |
| 1072 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1073 | } |
| 1074 | #if 0 |
| 1075 | if (list) { |
| 1076 | print_list(out, ctx, outformat_d); |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
| 1080 | ret = EXIT_SUCCESS; |
| 1081 | |
| 1082 | cleanup: |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1083 | ly_set_free(mods, NULL); |
| 1084 | ly_set_free(searchpaths, NULL); |
| 1085 | for (i = 0; i < featsize; i++) { |
| 1086 | free(feat[i]); |
| 1087 | } |
| 1088 | free(feat); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1089 | for (; data; data = data_item) { |
| 1090 | data_item = data->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1091 | lyd_free_all(data->tree); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1092 | free(data); |
| 1093 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1094 | ly_ctx_destroy(ctx, NULL); |
| 1095 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 1096 | lyp_free(out, NULL, 1); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1097 | return ret; |
| 1098 | } |