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; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 217 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 218 | } else if (!strcmp(ptr, "yin")) { |
| 219 | informat_s = LYS_IN_YIN; |
| 220 | informat_d = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 221 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 222 | } else if (!strcmp(ptr, "xml")) { |
| 223 | informat_s = 0; |
| 224 | informat_d = LYD_XML; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 225 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 226 | } else if (!strcmp(ptr, "json")) { |
| 227 | informat_s = 0; |
| 228 | informat_d = LYD_JSON; |
| 229 | #endif |
| 230 | } else { |
| 231 | fprintf(stderr, "yanglint error: input file in an unknown format \"%s\".\n", ptr); |
| 232 | return 0; |
| 233 | } |
| 234 | } else { |
| 235 | fprintf(stderr, "yanglint error: input file \"%s\" without file extension - unknown format.\n", filename); |
| 236 | return 0; |
| 237 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 238 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 239 | if (data) { |
| 240 | (*data) = informat_d; |
| 241 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 242 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 243 | if (schema) { |
| 244 | (*schema) = informat_s; |
| 245 | } |
| 246 | |
| 247 | if (informat_s) { |
| 248 | return 1; |
| 249 | } else { |
| 250 | return 2; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | int |
| 255 | main_ni(int argc, char* argv[]) |
| 256 | { |
| 257 | int ret = EXIT_FAILURE; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 258 | int opt, opt_index = 0, i, featsize = 0, compiled = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 259 | struct option options[] = { |
| 260 | #if 0 |
| 261 | {"auto", no_argument, NULL, 'a'}, |
| 262 | {"default", required_argument, NULL, 'd'}, |
| 263 | #endif |
| 264 | {"format", required_argument, NULL, 'f'}, |
| 265 | {"features", required_argument, NULL, 'F'}, |
| 266 | #if 0 |
| 267 | {"tree-print-groupings", no_argument, NULL, 'g'}, |
| 268 | {"tree-print-uses", no_argument, NULL, 'u'}, |
| 269 | {"tree-no-leafref-target", no_argument, NULL, 'n'}, |
| 270 | {"tree-path", required_argument, NULL, 'P'}, |
| 271 | {"tree-line-length", required_argument, NULL, 'L'}, |
| 272 | #endif |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 273 | {"compiled", no_argument, NULL, 'c'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 274 | {"help", no_argument, NULL, 'h'}, |
| 275 | #if 0 |
| 276 | {"tree-help", no_argument, NULL, 'H'}, |
| 277 | #endif |
| 278 | {"allimplemented", no_argument, NULL, 'i'}, |
| 279 | {"disable-cwd-search", no_argument, NULL, 'D'}, |
| 280 | {"list", no_argument, NULL, 'l'}, |
| 281 | #if 0 |
| 282 | {"merge", no_argument, NULL, 'm'}, |
| 283 | #endif |
| 284 | {"output", required_argument, NULL, 'o'}, |
| 285 | {"path", required_argument, NULL, 'p'}, |
| 286 | #if 0 |
| 287 | {"running", required_argument, NULL, 'r'}, |
| 288 | {"operational", required_argument, NULL, 'O'}, |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 289 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 290 | {"strict", no_argument, NULL, 's'}, |
| 291 | {"type", required_argument, NULL, 't'}, |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 292 | {"version", no_argument, NULL, 'v'}, |
| 293 | {"verbose", no_argument, NULL, 'V'}, |
| 294 | #ifndef NDEBUG |
| 295 | {"debug", required_argument, NULL, 'G'}, |
| 296 | #endif |
| 297 | {NULL, required_argument, NULL, 'y'}, |
| 298 | {NULL, 0, NULL, 0} |
| 299 | }; |
| 300 | FILE *out = stdout; |
| 301 | struct ly_ctx *ctx = NULL; |
| 302 | const struct lys_module *mod; |
| 303 | LYS_OUTFORMAT outformat_s = 0; |
| 304 | LYS_INFORMAT informat_s; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 305 | LYD_FORMAT informat_d, outformat_d = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 306 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 307 | LYD_FORMAT ylformat = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 308 | #endif |
| 309 | struct ly_set *searchpaths = NULL; |
| 310 | const char *outtarget_s = NULL; |
| 311 | char **feat = NULL, *ptr, *featlist, *dir; |
| 312 | struct stat st; |
| 313 | uint32_t u; |
| 314 | 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] | 315 | int autodetection = 0, options_parser = 0, merge = 0; |
| 316 | const char *oper_file = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 317 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 318 | const char *envelope_s = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 319 | char *ylpath = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 320 | int options_dflt = 0, envelope = 0; |
| 321 | struct lyxml_elem *iter, *elem; |
| 322 | struct *subroot, *next, *node; |
| 323 | #endif |
| 324 | struct lyd_node *oper = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 325 | struct dataitem { |
| 326 | const char *filename; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 327 | struct lyd_node *tree; |
| 328 | struct dataitem *next; |
| 329 | LYD_FORMAT format; |
| 330 | int type; |
| 331 | } *data = NULL, *data_item, *data_prev = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 332 | struct ly_set *mods = NULL; |
| 333 | void *p; |
| 334 | int index = 0; |
| 335 | |
| 336 | opterr = 0; |
| 337 | #ifndef NDEBUG |
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:vVG:y:", options, &opt_index)) != -1) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 339 | #else |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 340 | 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] | 341 | #endif |
| 342 | { |
| 343 | switch (opt) { |
| 344 | #if 0 |
| 345 | case 'a': |
| 346 | envelope = 1; |
| 347 | break; |
| 348 | case 'd': |
| 349 | if (!strcmp(optarg, "all")) { |
| 350 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL; |
| 351 | } else if (!strcmp(optarg, "all-tagged")) { |
| 352 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_ALL_TAG; |
| 353 | } else if (!strcmp(optarg, "trim")) { |
| 354 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_TRIM; |
| 355 | } else if (!strcmp(optarg, "implicit-tagged")) { |
| 356 | options_dflt = (options_dflt & ~LYP_WD_MASK) | LYP_WD_IMPL_TAG; |
| 357 | } else { |
| 358 | fprintf(stderr, "yanglint error: unknown default mode %s\n", optarg); |
| 359 | help(1); |
| 360 | goto cleanup; |
| 361 | } |
| 362 | break; |
| 363 | #endif |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 364 | case 'c': |
| 365 | compiled = 1; |
| 366 | break; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 367 | case 'f': |
| 368 | if (!strcasecmp(optarg, "yang")) { |
| 369 | outformat_s = LYS_OUT_YANG; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 370 | outformat_d = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 371 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 372 | } else if (!strcasecmp(optarg, "tree")) { |
| 373 | outformat_s = LYS_OUT_TREE; |
| 374 | outformat_d = 0; |
| 375 | } else if (!strcasecmp(optarg, "tree-rfc")) { |
| 376 | outformat_s = LYS_OUT_TREE; |
| 377 | outoptions_s |= LYS_OUTOPT_TREE_RFC; |
| 378 | outformat_d = 0; |
| 379 | } else if (!strcasecmp(optarg, "yin")) { |
| 380 | outformat_s = LYS_OUT_YIN; |
| 381 | outformat_d = 0; |
| 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")) { |
| 515 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK); |
| 516 | autodetection = 1; |
| 517 | } else if (!strcmp(optarg, "config")) { |
| 518 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 519 | } else if (!strcmp(optarg, "get")) { |
| 520 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 521 | } else if (!strcmp(optarg, "getconfig")) { |
| 522 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 523 | } else if (!strcmp(optarg, "edit")) { |
| 524 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 525 | } else if (!strcmp(optarg, "data")) { |
| 526 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB; |
| 527 | } else if (!strcmp(optarg, "rpc")) { |
| 528 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 529 | } else if (!strcmp(optarg, "rpcreply")) { |
| 530 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY; |
| 531 | } else if (!strcmp(optarg, "notif")) { |
| 532 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 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 | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 616 | if (compiled) { |
| 617 | if (!outformat_s) { |
| 618 | fprintf(stderr, "yanglint warning: --compiled option takes effect only in case of printing schemas.\n"); |
| 619 | } else { |
| 620 | outformat_s++; |
| 621 | } |
| 622 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 623 | if (outformat_s && outformat_s != LYS_OUT_TREE && (optind + 1) < argc) { |
| 624 | /* we have multiple schemas to be printed as YIN or YANG */ |
| 625 | fprintf(stderr, "yanglint error: too many schemas to convert and store.\n"); |
| 626 | goto cleanup; |
| 627 | } |
| 628 | if (outoptions_s || outtarget_s || outline_length_s) { |
| 629 | #if 0 |
| 630 | if (outformat_d || (outformat_s && outformat_s != LYS_OUT_TREE)) { |
| 631 | /* we have --tree-print-grouping with other output format than tree */ |
| 632 | fprintf(stderr, |
| 633 | "yanglint warning: --tree options take effect only in case of the tree output format.\n"); |
| 634 | } |
| 635 | } |
| 636 | if (merge) { |
| 637 | if (autodetection || (options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF))) { |
| 638 | fprintf(stderr, "yanglint warning: merging not allowed, ignoring option -m.\n"); |
| 639 | merge = 0; |
| 640 | } else { |
| 641 | /* first, files will be parsed as trusted to allow missing data, then the data trees will be merged |
| 642 | * and the result will be validated */ |
| 643 | options_parser |= LYD_OPT_TRUSTED; |
| 644 | } |
| 645 | #endif |
| 646 | } |
| 647 | #if 0 |
| 648 | if (!outformat_d && options_dflt) { |
| 649 | /* we have options for printing default nodes, but data output not specified */ |
| 650 | fprintf(stderr, "yanglint warning: default mode is ignored when not printing data.\n"); |
| 651 | } |
| 652 | if (outformat_s && (options_parser || autodetection)) { |
| 653 | /* we have options for printing data tree, but output is schema */ |
| 654 | fprintf(stderr, "yanglint warning: data parser options are ignored when printing schema.\n"); |
| 655 | } |
| 656 | if (oper_file && (!autodetection && !(options_parser & (LYD_OPT_RPC | LYD_OPT_RPCREPLY | LYD_OPT_NOTIF)))) { |
| 657 | fprintf(stderr, "yanglint warning: operational datastore applies only to RPCs or Notifications.\n"); |
| 658 | /* ignore operational datastore file */ |
| 659 | oper_file = NULL; |
| 660 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 661 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 662 | if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_DATA) { |
| 663 | /* add option to ignore ietf-yang-library data for implicit data type */ |
| 664 | options_parser |= LYD_OPT_DATA_NO_YANGLIB; |
| 665 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 666 | |
| 667 | /* set callback for printing libyang messages */ |
| 668 | ly_set_log_clb(libyang_verbclb, 1); |
| 669 | #if 0 |
| 670 | /* create libyang context */ |
| 671 | if (ylpath) { |
| 672 | ctx = ly_ctx_new_ylpath(searchpaths ? (const char*)searchpaths->set.g[0] : NULL, ylpath, ylformat, options_ctx); |
| 673 | } else { |
| 674 | #else |
| 675 | { |
| 676 | #endif |
| 677 | ly_ctx_new(NULL, options_ctx, &ctx); |
| 678 | } |
| 679 | if (!ctx) { |
| 680 | goto cleanup; |
| 681 | } |
| 682 | |
| 683 | /* set searchpaths */ |
| 684 | if (searchpaths) { |
| 685 | for (u = 0; u < searchpaths->count; u++) { |
| 686 | ly_ctx_set_searchdir(ctx, (const char*)searchpaths->objs[u]); |
| 687 | } |
| 688 | index = u + 1; |
| 689 | } |
| 690 | |
| 691 | /* derefered setting of verbosity in libyang after context initiation */ |
| 692 | ly_verb(verbose); |
| 693 | |
| 694 | mods = ly_set_new(); |
| 695 | |
| 696 | |
| 697 | /* divide input files */ |
| 698 | for (i = 0; i < argc - optind; i++) { |
| 699 | /* get the file format */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 700 | if (!get_fileformat(argv[optind + i], &informat_s, &informat_d)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 701 | goto cleanup; |
| 702 | } |
| 703 | |
| 704 | if (informat_s) { |
| 705 | /* load/validate schema */ |
| 706 | if (verbose >= 2) { |
| 707 | fprintf(stdout, "Validating %s schema file.\n", argv[optind + i]); |
| 708 | } |
| 709 | dir = strdup(argv[optind + i]); |
| 710 | ly_ctx_set_searchdir(ctx, ptr = dirname(dir)); |
| 711 | mod = lys_parse_path(ctx, argv[optind + i], informat_s); |
| 712 | ly_ctx_unset_searchdir(ctx, index); |
| 713 | free(dir); |
| 714 | if (!mod) { |
| 715 | goto cleanup; |
| 716 | } |
| 717 | ly_set_add(mods, (void *)mod, 0); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 718 | } else { |
| 719 | if (autodetection && informat_d != LYD_XML) { |
| 720 | /* data file content autodetection is possible only for XML input */ |
| 721 | fprintf(stderr, "yanglint error: data type autodetection is applicable only to XML files.\n"); |
| 722 | goto cleanup; |
| 723 | } |
| 724 | |
| 725 | /* remember data filename and its format */ |
| 726 | if (!data) { |
| 727 | data = data_item = malloc(sizeof *data); |
| 728 | } else { |
| 729 | for (data_item = data; data_item->next; data_item = data_item->next); |
| 730 | data_item->next = malloc(sizeof *data_item); |
| 731 | data_item = data_item->next; |
| 732 | } |
| 733 | data_item->filename = argv[optind + i]; |
| 734 | data_item->format = informat_d; |
| 735 | data_item->type = options_parser & LYD_OPT_TYPEMASK; |
| 736 | data_item->tree = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 737 | data_item->next = NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 738 | } |
| 739 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 740 | if (outformat_d && !data && !list) { |
| 741 | fprintf(stderr, "yanglint error: no input data file for the specified data output format.\n"); |
| 742 | goto cleanup; |
| 743 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 744 | |
| 745 | /* enable specified features, if not specified, all the module's features are enabled */ |
| 746 | u = 4; /* skip internal libyang modules */ |
| 747 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
| 748 | for (i = 0; i < featsize; i++) { |
| 749 | if (!strcmp(feat[i], mod->name)) { |
| 750 | /* parse features spec */ |
| 751 | featlist = strdup(feat[i] + strlen(feat[i]) + 1); |
| 752 | ptr = NULL; |
| 753 | while((ptr = strtok(ptr ? NULL : featlist, ","))) { |
| 754 | if (verbose >= 2) { |
| 755 | fprintf(stdout, "Enabling feature %s in module %s.\n", ptr, mod->name); |
| 756 | } |
| 757 | if (lys_feature_enable(mod, ptr)) { |
| 758 | fprintf(stderr, "Feature %s not defined in module %s.\n", ptr, mod->name); |
| 759 | } |
| 760 | } |
| 761 | free(featlist); |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | if (i == featsize) { |
| 766 | if (verbose >= 2) { |
| 767 | fprintf(stdout, "Enabling all features in module %s.\n", mod->name); |
| 768 | } |
| 769 | lys_feature_enable(mod, "*"); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* convert (print) to FORMAT */ |
| 774 | if (outformat_s) { |
| 775 | if (outformat_s == LYS_OUT_JSON && mods->count > 1) { |
| 776 | fputs("[", out); |
| 777 | } |
| 778 | for (u = 0; u < mods->count; u++) { |
| 779 | if (u) { |
| 780 | if (outformat_s == LYS_OUT_JSON) { |
| 781 | fputs(",\n", out); |
| 782 | } else { |
| 783 | fputs("\n", out); |
| 784 | } |
| 785 | } |
| 786 | lys_print_file(out, (struct lys_module *)mods->objs[u], outformat_s, outline_length_s, outoptions_s); |
| 787 | } |
| 788 | if (outformat_s == LYS_OUT_JSON) { |
| 789 | if (mods->count > 1) { |
| 790 | fputs("]\n", out); |
| 791 | } else if (mods->count == 1) { |
| 792 | fputs("\n", out); |
| 793 | } |
| 794 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 795 | } else if (data) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 796 | |
| 797 | /* prepare operational datastore when specified for RPC/Notification */ |
| 798 | if (oper_file) { |
| 799 | /* get the file format */ |
| 800 | if (!get_fileformat(oper_file, NULL, &informat_d)) { |
| 801 | goto cleanup; |
| 802 | } else if (!informat_d) { |
| 803 | fprintf(stderr, "yanglint error: The operational data are expected in XML or JSON format.\n"); |
| 804 | goto cleanup; |
| 805 | } |
| 806 | oper = lyd_parse_path(ctx, oper_file, informat_d, LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_TRUSTED); |
| 807 | if (!oper) { |
| 808 | fprintf(stderr, "yanglint error: Failed to parse the operational datastore file for RPC/Notification validation.\n"); |
| 809 | goto cleanup; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | for (data_item = data, data_prev = NULL; data_item; data_prev = data_item, data_item = data_item->next) { |
| 814 | /* 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] | 815 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 816 | if (autodetection) { |
| 817 | /* erase option not covered by LYD_OPT_TYPEMASK, but used according to the type */ |
| 818 | options_parser &= ~LYD_OPT_DATA_NO_YANGLIB; |
| 819 | /* automatically detect data type from the data top level */ |
| 820 | data_item->xml = lyxml_parse_path(ctx, data_item->filename, 0); |
| 821 | if (!data_item->xml) { |
| 822 | fprintf(stderr, "yanglint error: parsing XML data for data type autodetection failed.\n"); |
| 823 | goto cleanup; |
| 824 | } |
| 825 | |
| 826 | /* NOTE: namespace is ignored to simplify usage of this feature */ |
| 827 | if (!strcmp(data_item->xml->name, "data")) { |
| 828 | if (verbose >= 2) { |
| 829 | fprintf(stdout, "Parsing %s as complete datastore.\n", data_item->filename); |
| 830 | } |
| 831 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_DATA_NO_YANGLIB; |
| 832 | data_item->type = LYD_OPT_DATA; |
| 833 | } else if (!strcmp(data_item->xml->name, "config")) { |
| 834 | if (verbose >= 2) { |
| 835 | fprintf(stdout, "Parsing %s as config data.\n", data_item->filename); |
| 836 | } |
| 837 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_CONFIG; |
| 838 | data_item->type = LYD_OPT_CONFIG; |
| 839 | } else if (!strcmp(data_item->xml->name, "get-reply")) { |
| 840 | if (verbose >= 2) { |
| 841 | fprintf(stdout, "Parsing %s as <get> reply data.\n", data_item->filename); |
| 842 | } |
| 843 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GET; |
| 844 | data_item->type = LYD_OPT_GET; |
| 845 | } else if (!strcmp(data_item->xml->name, "get-config-reply")) { |
| 846 | if (verbose >= 2) { |
| 847 | fprintf(stdout, "Parsing %s as <get-config> reply data.\n", data_item->filename); |
| 848 | } |
| 849 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_GETCONFIG; |
| 850 | data_item->type = LYD_OPT_GETCONFIG; |
| 851 | } else if (!strcmp(data_item->xml->name, "edit-config")) { |
| 852 | if (verbose >= 2) { |
| 853 | fprintf(stdout, "Parsing %s as <edit-config> data.\n", data_item->filename); |
| 854 | } |
| 855 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_EDIT; |
| 856 | data_item->type = LYD_OPT_EDIT; |
| 857 | } else if (!strcmp(data_item->xml->name, "rpc")) { |
| 858 | if (verbose >= 2) { |
| 859 | fprintf(stdout, "Parsing %s as <rpc> data.\n", data_item->filename); |
| 860 | } |
| 861 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC; |
| 862 | data_item->type = LYD_OPT_RPC; |
| 863 | } else if (!strcmp(data_item->xml->name, "rpc-reply")) { |
| 864 | if (verbose >= 2) { |
| 865 | fprintf(stdout, "Parsing %s as <rpc-reply> data.\n", data_item->filename); |
| 866 | } |
| 867 | |
| 868 | data_item->type = LYD_OPT_RPCREPLY; |
| 869 | if (!data_item->next || (data_prev && !data_prev->tree)) { |
| 870 | fprintf(stderr, "RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename); |
| 871 | goto cleanup; |
| 872 | } |
| 873 | |
| 874 | continue; |
| 875 | } else if (!strcmp(data_item->xml->name, "notification")) { |
| 876 | if (verbose >= 2) { |
| 877 | fprintf(stdout, "Parsing %s as <notification> data.\n", data_item->filename); |
| 878 | } |
| 879 | options_parser = (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_NOTIF; |
| 880 | data_item->type = LYD_OPT_NOTIF; |
| 881 | |
| 882 | /* ignore eventTime element if present */ |
| 883 | while (data_item->xml->child && !strcmp(data_item->xml->child->name, "eventTime")) { |
| 884 | lyxml_free(ctx, data_item->xml->child); |
| 885 | } |
| 886 | } else { |
| 887 | fprintf(stderr, "yanglint error: invalid top-level element \"%s\" for data type autodetection.\n", |
| 888 | data_item->xml->name); |
| 889 | goto cleanup; |
| 890 | } |
| 891 | |
| 892 | data_item->tree = lyd_parse_xml(ctx, &data_item->xml->child, options_parser, oper); |
| 893 | if (data_prev && data_prev->type == LYD_OPT_RPCREPLY) { |
| 894 | parse_reply: |
| 895 | /* check result of the RPC parsing, we are going to do another parsing in this step */ |
| 896 | if (ly_errno) { |
| 897 | goto cleanup; |
| 898 | } |
| 899 | |
| 900 | /* check that we really have RPC for the reply */ |
| 901 | if (data_item->type != LYD_OPT_RPC) { |
| 902 | fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_prev->filename); |
| 903 | goto cleanup; |
| 904 | } |
| 905 | |
| 906 | if (data_prev->format == LYD_XML) { |
| 907 | /* ignore <ok> and <rpc-error> elements if present */ |
| 908 | u = 0; |
| 909 | LY_TREE_FOR_SAFE(data_prev->xml->child, iter, elem) { |
| 910 | if (!strcmp(data_prev->xml->child->name, "ok")) { |
| 911 | if (u) { |
| 912 | /* rpc-error or ok already present */ |
| 913 | u = 0x8; /* error flag */ |
| 914 | } else { |
| 915 | u = 0x1 | 0x4; /* <ok> flag with lyxml_free() flag */ |
| 916 | } |
| 917 | } else if (!strcmp(data_prev->xml->child->name, "rpc-error")) { |
| 918 | if (u && (u & 0x1)) { |
| 919 | /* ok already present, rpc-error can be present multiple times */ |
| 920 | u = 0x8; /* error flag */ |
| 921 | } else { |
| 922 | u = 0x2 | 0x4; /* <rpc-error> flag with lyxml_free() flag */ |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | if (u == 0x8) { |
| 927 | fprintf(stderr, "yanglint error: Invalid RPC reply (%s) content.\n", data_prev->filename); |
| 928 | goto cleanup; |
| 929 | } else if (u & 0x4) { |
| 930 | lyxml_free(ctx, data_prev->xml->child); |
| 931 | u &= ~0x4; /* unset lyxml_free() flag */ |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | /* finally, parse RPC reply from the previous step */ |
| 936 | data_prev->tree = lyd_parse_xml(ctx, &data_prev->xml->child, |
| 937 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper); |
| 938 | } else { /* LYD_JSON */ |
| 939 | data_prev->tree = lyd_parse_path(ctx, data_prev->filename, data_item->format, |
| 940 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPCREPLY, data_item->tree, oper); |
| 941 | } |
| 942 | } |
| 943 | } else if ((options_parser & LYD_OPT_TYPEMASK) == LYD_OPT_RPCREPLY) { |
| 944 | if (data_prev && !data_prev->tree) { |
| 945 | /* now we should have RPC for the preceding RPC reply */ |
| 946 | data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, |
| 947 | (options_parser & ~LYD_OPT_TYPEMASK) | LYD_OPT_RPC, oper); |
| 948 | data_item->type = LYD_OPT_RPC; |
| 949 | goto parse_reply; |
| 950 | } else { |
| 951 | /* now we have RPC reply which will be parsed in next step together with its RPC */ |
| 952 | if (!data_item->next) { |
| 953 | fprintf(stderr, "yanglint error: RPC reply (%s) must be paired with the original RPC, see help.\n", data_item->filename); |
| 954 | goto cleanup; |
| 955 | } |
| 956 | if (data_item->format == LYD_XML) { |
| 957 | /* create rpc-reply container to unify handling with autodetection */ |
| 958 | data_item->xml = calloc(1, sizeof *data_item->xml); |
| 959 | if (!data_item->xml) { |
| 960 | fprintf(stderr, "yanglint error: Memory allocation failed failed.\n"); |
| 961 | goto cleanup; |
| 962 | } |
| 963 | data_item->xml->name = lydict_insert(ctx, "rpc-reply", 9); |
| 964 | data_item->xml->prev = data_item->xml; |
| 965 | data_item->xml->child = lyxml_parse_path(ctx, data_item->filename, LYXML_PARSE_MULTIROOT | LYXML_PARSE_NOMIXEDCONTENT); |
| 966 | if (data_item->xml->child) { |
| 967 | data_item->xml->child->parent = data_item->xml; |
| 968 | } |
| 969 | } |
| 970 | continue; |
| 971 | } |
| 972 | } else { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 973 | #else |
| 974 | { |
| 975 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 976 | data_item->tree = lyd_parse_path(ctx, data_item->filename, data_item->format, options_parser, oper); |
| 977 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 978 | if (ly_err_first(ctx)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 979 | goto cleanup; |
| 980 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 981 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 982 | if (merge && data != data_item) { |
| 983 | if (!data->tree) { |
| 984 | data->tree = data_item->tree; |
| 985 | } else if (data_item->tree) { |
| 986 | /* merge results */ |
| 987 | if (lyd_merge(data->tree, data_item->tree, LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) { |
| 988 | fprintf(stderr, "yanglint error: merging multiple data trees failed.\n"); |
| 989 | goto cleanup; |
| 990 | } |
| 991 | } |
| 992 | data_item->tree = NULL; |
| 993 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 994 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 995 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 996 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 997 | if (merge) { |
| 998 | /* validate the merged data tree, do not trust the input, invalidate all the data first */ |
| 999 | LY_TREE_FOR(data->tree, subroot) { |
| 1000 | LY_TREE_DFS_BEGIN(subroot, next, node) { |
| 1001 | node->validity = LYD_VAL_OK; |
| 1002 | switch (node->schema->nodetype) { |
| 1003 | case LYS_LEAFLIST: |
| 1004 | case LYS_LEAF: |
| 1005 | if (((struct lys_node_leaf *)node->schema)->type.base == LY_TYPE_LEAFREF) { |
| 1006 | node->validity |= LYD_VAL_LEAFREF; |
| 1007 | } |
| 1008 | break; |
| 1009 | case LYS_LIST: |
| 1010 | node->validity |= LYD_VAL_UNIQUE; |
| 1011 | /* falls through */ |
| 1012 | case LYS_CONTAINER: |
| 1013 | case LYS_NOTIF: |
| 1014 | case LYS_RPC: |
| 1015 | case LYS_ACTION: |
| 1016 | node->validity |= LYD_VAL_MAND; |
| 1017 | break; |
| 1018 | default: |
| 1019 | break; |
| 1020 | } |
| 1021 | LY_TREE_DFS_END(subroot, next, node) |
| 1022 | } |
| 1023 | } |
| 1024 | if (lyd_validate(&data->tree, options_parser & ~LYD_OPT_TRUSTED, ctx)) { |
| 1025 | goto cleanup; |
| 1026 | } |
| 1027 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1028 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1029 | /* print only if data output format specified */ |
| 1030 | if (outformat_d) { |
| 1031 | for (data_item = data; data_item; data_item = data_item->next) { |
| 1032 | if (!merge && verbose >= 2) { |
| 1033 | fprintf(stdout, "File %s:\n", data_item->filename); |
| 1034 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1035 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1036 | if (outformat_d == LYD_XML && envelope) { |
| 1037 | switch (data_item->type) { |
| 1038 | case LYD_OPT_DATA: |
| 1039 | envelope_s = "data"; |
| 1040 | break; |
| 1041 | case LYD_OPT_CONFIG: |
| 1042 | envelope_s = "config"; |
| 1043 | break; |
| 1044 | case LYD_OPT_GET: |
| 1045 | envelope_s = "get-reply"; |
| 1046 | break; |
| 1047 | case LYD_OPT_GETCONFIG: |
| 1048 | envelope_s = "get-config-reply"; |
| 1049 | break; |
| 1050 | case LYD_OPT_EDIT: |
| 1051 | envelope_s = "edit-config"; |
| 1052 | break; |
| 1053 | case LYD_OPT_RPC: |
| 1054 | envelope_s = "rpc"; |
| 1055 | break; |
| 1056 | case LYD_OPT_RPCREPLY: |
| 1057 | envelope_s = "rpc-reply"; |
| 1058 | break; |
| 1059 | case LYD_OPT_NOTIF: |
| 1060 | envelope_s = "notification"; |
| 1061 | break; |
| 1062 | } |
| 1063 | fprintf(out, "<%s>\n", envelope_s); |
| 1064 | if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) { |
| 1065 | /* action */ |
| 1066 | fprintf(out, "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n"); |
| 1067 | } |
| 1068 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1069 | #endif |
| 1070 | lyd_print_file(out, (data_item->type == LYD_OPT_RPCREPLY) ? lyd_node_children(data_item->tree) : data_item->tree, |
| 1071 | outformat_d, LYDP_WITHSIBLINGS | LYDP_FORMAT /* TODO defaults | options_dflt */); |
| 1072 | #if 0 |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1073 | if (envelope_s) { |
| 1074 | if (data_item->type == LYD_OPT_RPC && data_item->tree->schema->nodetype != LYS_RPC) { |
| 1075 | fprintf(out, "</action>\n"); |
| 1076 | } |
| 1077 | fprintf(out, "</%s>\n", envelope_s); |
| 1078 | } |
| 1079 | if (merge) { |
| 1080 | /* stop after first item */ |
| 1081 | break; |
| 1082 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1083 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1084 | } |
| 1085 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1086 | } |
| 1087 | #if 0 |
| 1088 | if (list) { |
| 1089 | print_list(out, ctx, outformat_d); |
| 1090 | } |
| 1091 | #endif |
| 1092 | |
| 1093 | ret = EXIT_SUCCESS; |
| 1094 | |
| 1095 | cleanup: |
| 1096 | if (out && out != stdout) { |
| 1097 | fclose(out); |
| 1098 | } |
| 1099 | ly_set_free(mods, NULL); |
| 1100 | ly_set_free(searchpaths, NULL); |
| 1101 | for (i = 0; i < featsize; i++) { |
| 1102 | free(feat[i]); |
| 1103 | } |
| 1104 | free(feat); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1105 | for (; data; data = data_item) { |
| 1106 | data_item = data->next; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1107 | lyd_free_all(data->tree); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1108 | free(data); |
| 1109 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1110 | lyd_free_all(oper); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1111 | ly_ctx_destroy(ctx, NULL); |
| 1112 | |
| 1113 | return ret; |
| 1114 | } |