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