Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file common.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool - common functions for both interactive and non-interactive mode. |
| 5 | * |
| 6 | * Copyright (c) 2020 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 |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 17 | |
| 18 | #include "common.h" |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <errno.h> |
| 22 | #include <getopt.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include "compat.h" |
| 29 | #include "libyang.h" |
| 30 | |
| 31 | int |
| 32 | parse_schema_path(const char *path, char **dir, char **module) |
| 33 | { |
| 34 | char *p; |
| 35 | |
| 36 | assert(dir); |
| 37 | assert(module); |
| 38 | |
| 39 | /* split the path to dirname and basename for further work */ |
| 40 | *dir = strdup(path); |
| 41 | *module = strrchr(*dir, '/'); |
| 42 | if (!(*module)) { |
| 43 | *module = *dir; |
| 44 | *dir = strdup("./"); |
| 45 | } else { |
| 46 | *module[0] = '\0'; /* break the dir */ |
| 47 | *module = strdup((*module) + 1); |
| 48 | } |
| 49 | /* get the pure module name without suffix or revision part of the filename */ |
| 50 | if ((p = strchr(*module, '@'))) { |
| 51 | /* revision */ |
| 52 | *p = '\0'; |
| 53 | } else if ((p = strrchr(*module, '.'))) { |
| 54 | /* fileformat suffix */ |
| 55 | *p = '\0'; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int |
| 62 | get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in) |
| 63 | { |
| 64 | struct stat st; |
| 65 | |
| 66 | /* check that the filepath exists and is a regular file */ |
| 67 | if (stat(filepath, &st) == -1) { |
| 68 | YLMSG_E("Unable to use input filepath (%s) - %s.\n", filepath, strerror(errno)); |
| 69 | return -1; |
| 70 | } |
| 71 | if (!S_ISREG(st.st_mode)) { |
| 72 | YLMSG_E("Provided input file (%s) is not a regular file.\n", filepath); |
| 73 | return -1; |
| 74 | } |
| 75 | |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 76 | if ((format_schema && !*format_schema) || (format_data && !*format_data)) { |
| 77 | /* get the file format */ |
| 78 | if (get_format(filepath, format_schema, format_data)) { |
| 79 | return -1; |
| 80 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (ly_in_new_filepath(filepath, 0, in)) { |
| 84 | YLMSG_E("Unable to process input file.\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | free_features(void *flist) |
| 93 | { |
| 94 | struct schema_features *rec = (struct schema_features *)flist; |
| 95 | |
| 96 | if (rec) { |
Michal Vasko | a9a9861 | 2021-11-22 10:00:27 +0100 | [diff] [blame] | 97 | free(rec->mod_name); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 98 | if (rec->features) { |
| 99 | for (uint32_t u = 0; rec->features[u]; ++u) { |
| 100 | free(rec->features[u]); |
| 101 | } |
| 102 | free(rec->features); |
| 103 | } |
| 104 | free(rec); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | get_features(struct ly_set *fset, const char *module, const char ***features) |
| 110 | { |
| 111 | /* get features list for this module */ |
| 112 | for (uint32_t u = 0; u < fset->count; ++u) { |
| 113 | struct schema_features *sf = (struct schema_features *)fset->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 114 | |
Michal Vasko | a9a9861 | 2021-11-22 10:00:27 +0100 | [diff] [blame] | 115 | if (!strcmp(module, sf->mod_name)) { |
Radek Krejci | 8143bb4 | 2021-04-07 11:43:50 +0200 | [diff] [blame] | 116 | /* matched module - explicitly set features */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 117 | *features = (const char **)sf->features; |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 118 | sf->applied = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | } |
Radek Krejci | 8143bb4 | 2021-04-07 11:43:50 +0200 | [diff] [blame] | 122 | |
Michal Vasko | 5974087 | 2021-11-22 10:01:34 +0100 | [diff] [blame] | 123 | /* features not set so disable all */ |
| 124 | *features = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int |
| 128 | parse_features(const char *fstring, struct ly_set *fset) |
| 129 | { |
| 130 | struct schema_features *rec; |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 131 | uint32_t count; |
| 132 | char *p, **fp; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 133 | |
| 134 | rec = calloc(1, sizeof *rec); |
| 135 | if (!rec) { |
| 136 | YLMSG_E("Unable to allocate features information record (%s).\n", strerror(errno)); |
| 137 | return -1; |
| 138 | } |
| 139 | if (ly_set_add(fset, rec, 1, NULL)) { |
| 140 | YLMSG_E("Unable to store features information (%s).\n", strerror(errno)); |
| 141 | free(rec); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | /* fill the record */ |
| 146 | p = strchr(fstring, ':'); |
| 147 | if (!p) { |
Michal Vasko | 9a5f3dd | 2021-11-23 08:59:53 +0100 | [diff] [blame] | 148 | YLMSG_E("Invalid format of the features specification (%s).\n", fstring); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 149 | return -1; |
| 150 | } |
Michal Vasko | a9a9861 | 2021-11-22 10:00:27 +0100 | [diff] [blame] | 151 | rec->mod_name = strndup(fstring, p - fstring); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 152 | |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 153 | count = 0; |
| 154 | while (p) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 155 | size_t len = 0; |
| 156 | char *token = p + 1; |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 157 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 158 | p = strchr(token, ','); |
| 159 | if (!p) { |
| 160 | /* the last item, if any */ |
| 161 | len = strlen(token); |
| 162 | } else { |
| 163 | len = p - token; |
| 164 | } |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 165 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 166 | if (len) { |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 167 | fp = realloc(rec->features, (count + 1) * sizeof *rec->features); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 168 | if (!fp) { |
| 169 | YLMSG_E("Unable to store features list information (%s).\n", strerror(errno)); |
| 170 | return -1; |
| 171 | } |
| 172 | rec->features = fp; |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 173 | fp = &rec->features[count++]; /* array item to set */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 174 | (*fp) = strndup(token, len); |
| 175 | } |
| 176 | } |
| 177 | |
Michal Vasko | e699f7b | 2022-05-09 10:49:57 +0200 | [diff] [blame] | 178 | /* terminating NULL */ |
| 179 | fp = realloc(rec->features, (count + 1) * sizeof *rec->features); |
| 180 | if (!fp) { |
| 181 | YLMSG_E("Unable to store features list information (%s).\n", strerror(errno)); |
| 182 | return -1; |
| 183 | } |
| 184 | rec->features = fp; |
| 185 | rec->features[count++] = NULL; |
| 186 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | struct cmdline_file * |
| 191 | fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format) |
| 192 | { |
| 193 | struct cmdline_file *rec; |
| 194 | |
| 195 | rec = malloc(sizeof *rec); |
| 196 | if (!rec) { |
| 197 | YLMSG_E("Allocating memory for data file information failed.\n"); |
| 198 | return NULL; |
| 199 | } |
| 200 | if (set && ly_set_add(set, rec, 1, NULL)) { |
| 201 | free(rec); |
Michal Vasko | 7edebb4 | 2021-01-25 14:18:46 +0100 | [diff] [blame] | 202 | YLMSG_E("Storing data file information failed.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 203 | return NULL; |
| 204 | } |
| 205 | rec->in = in; |
| 206 | rec->path = path; |
| 207 | rec->format = format; |
| 208 | |
| 209 | return rec; |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | free_cmdline_file(void *cmdline_file) |
| 214 | { |
| 215 | struct cmdline_file *rec = (struct cmdline_file *)cmdline_file; |
| 216 | |
| 217 | if (rec) { |
| 218 | ly_in_free(rec->in, 1); |
| 219 | free(rec); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | free_cmdline(char *argv[]) |
| 225 | { |
| 226 | if (argv) { |
| 227 | free(argv[0]); |
| 228 | free(argv); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int |
| 233 | parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]) |
| 234 | { |
| 235 | int count; |
| 236 | char **vector; |
| 237 | char *ptr; |
| 238 | char qmark = 0; |
| 239 | |
| 240 | assert(cmdline); |
| 241 | assert(argc_p); |
| 242 | assert(argv_p); |
| 243 | |
| 244 | /* init */ |
| 245 | optind = 0; /* reinitialize getopt() */ |
| 246 | count = 1; |
| 247 | vector = malloc((count + 1) * sizeof *vector); |
| 248 | vector[0] = strdup(cmdline); |
| 249 | |
| 250 | /* command name */ |
| 251 | strtok(vector[0], " "); |
| 252 | |
| 253 | /* arguments */ |
| 254 | while ((ptr = strtok(NULL, " "))) { |
| 255 | size_t len; |
| 256 | void *r; |
| 257 | |
| 258 | len = strlen(ptr); |
| 259 | |
| 260 | if (qmark) { |
| 261 | /* still in quotated text */ |
| 262 | /* remove NULL termination of the previous token since it is not a token, |
| 263 | * but a part of the quotation string */ |
| 264 | ptr[-1] = ' '; |
| 265 | |
| 266 | if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) { |
| 267 | /* end of quotation */ |
| 268 | qmark = 0; |
| 269 | /* shorten the argument by the terminating quotation mark */ |
| 270 | ptr[len - 1] = '\0'; |
| 271 | } |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | /* another token in cmdline */ |
| 276 | ++count; |
| 277 | r = realloc(vector, (count + 1) * sizeof *vector); |
| 278 | if (!r) { |
Michal Vasko | 9a5f3dd | 2021-11-23 08:59:53 +0100 | [diff] [blame] | 279 | YLMSG_E("Memory allocation failed (%s:%d, %s).\n", __FILE__, __LINE__, strerror(errno)); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 280 | free(vector); |
| 281 | return -1; |
| 282 | } |
| 283 | vector = r; |
| 284 | vector[count - 1] = ptr; |
| 285 | |
| 286 | if ((ptr[0] == '"') || (ptr[0] == '\'')) { |
| 287 | /* remember the quotation mark to identify end of quotation */ |
| 288 | qmark = ptr[0]; |
| 289 | |
| 290 | /* move the remembered argument after the quotation mark */ |
| 291 | ++vector[count - 1]; |
| 292 | |
| 293 | /* check if the quotation is terminated within this token */ |
| 294 | if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) { |
| 295 | /* end of quotation */ |
| 296 | qmark = 0; |
| 297 | /* shorten the argument by the terminating quotation mark */ |
| 298 | ptr[len - 1] = '\0'; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | vector[count] = NULL; |
| 303 | |
| 304 | *argc_p = count; |
| 305 | *argv_p = vector; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data) |
| 312 | { |
| 313 | char *ptr; |
| 314 | LYS_INFORMAT informat_s; |
| 315 | LYD_FORMAT informat_d; |
| 316 | |
| 317 | /* get the file format */ |
| 318 | if ((ptr = strrchr(filename, '.')) != NULL) { |
| 319 | ++ptr; |
| 320 | if (!strcmp(ptr, "yang")) { |
| 321 | informat_s = LYS_IN_YANG; |
| 322 | informat_d = 0; |
| 323 | } else if (!strcmp(ptr, "yin")) { |
| 324 | informat_s = LYS_IN_YIN; |
| 325 | informat_d = 0; |
| 326 | } else if (!strcmp(ptr, "xml")) { |
| 327 | informat_s = 0; |
| 328 | informat_d = LYD_XML; |
| 329 | } else if (!strcmp(ptr, "json")) { |
| 330 | informat_s = 0; |
| 331 | informat_d = LYD_JSON; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 332 | } else if (!strcmp(ptr, "lyb")) { |
| 333 | informat_s = 0; |
| 334 | informat_d = LYD_LYB; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 335 | } else { |
| 336 | YLMSG_E("Input file \"%s\" in an unknown format \"%s\".\n", filename, ptr); |
| 337 | return 0; |
| 338 | } |
| 339 | } else { |
| 340 | YLMSG_E("Input file \"%s\" without file extension - unknown format.\n", filename); |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | if (informat_d) { |
| 345 | if (!data) { |
| 346 | YLMSG_E("Input file \"%s\" not expected to contain data instances (unexpected format).\n", filename); |
| 347 | return 2; |
| 348 | } |
| 349 | (*data) = informat_d; |
| 350 | } else if (informat_s) { |
| 351 | if (!schema) { |
| 352 | YLMSG_E("Input file \"%s\" not expected to contain schema definition (unexpected format).\n", filename); |
| 353 | return 3; |
| 354 | } |
| 355 | (*schema) = informat_s; |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | int |
| 362 | print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat) |
| 363 | { |
| 364 | struct lyd_node *ylib; |
| 365 | uint32_t idx = 0, has_modules = 0; |
| 366 | const struct lys_module *mod; |
| 367 | |
| 368 | if (outformat != LYD_UNKNOWN) { |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 369 | if (ly_ctx_get_yanglib_data(ctx, &ylib, "%u", ly_ctx_get_change_count(ctx))) { |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 370 | YLMSG_E("Getting context info (ietf-yang-library data) failed. If the YANG module is missing or not implemented, use an option to add it internally.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | lyd_print_all(out, ylib, outformat, 0); |
| 375 | lyd_free_all(ylib); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* iterate schemas in context and provide just the basic info */ |
| 380 | ly_print(out, "List of the loaded models:\n"); |
| 381 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
| 382 | has_modules++; |
| 383 | |
| 384 | /* conformance print */ |
| 385 | if (mod->implemented) { |
| 386 | ly_print(out, " I"); |
| 387 | } else { |
| 388 | ly_print(out, " i"); |
| 389 | } |
| 390 | |
| 391 | /* module print */ |
| 392 | ly_print(out, " %s", mod->name); |
| 393 | if (mod->revision) { |
| 394 | ly_print(out, "@%s", mod->revision); |
| 395 | } |
| 396 | |
| 397 | /* submodules print */ |
| 398 | if (mod->parsed && mod->parsed->includes) { |
| 399 | uint64_t u = 0; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 400 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 401 | ly_print(out, " ("); |
| 402 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 403 | ly_print(out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name); |
| 404 | if (mod->parsed->includes[u].rev[0]) { |
| 405 | ly_print(out, "@%s", mod->parsed->includes[u].rev); |
| 406 | } |
| 407 | } |
| 408 | ly_print(out, ")"); |
| 409 | } |
| 410 | |
| 411 | /* finish the line */ |
| 412 | ly_print(out, "\n"); |
| 413 | } |
| 414 | |
| 415 | if (!has_modules) { |
| 416 | ly_print(out, "\t(none)\n"); |
| 417 | } |
| 418 | |
| 419 | ly_print_flush(out); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 424 | evaluate_xpath(const struct lyd_node *tree, const char *xpath) |
| 425 | { |
Radek Krejci | 89757c1 | 2020-11-26 12:07:47 +0100 | [diff] [blame] | 426 | struct ly_set *set = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 427 | |
| 428 | if (lyd_find_xpath(tree, xpath, &set)) { |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | /* print result */ |
| 433 | printf("XPath \"%s\" evaluation result:\n", xpath); |
| 434 | if (!set->count) { |
| 435 | printf("\tEmpty\n"); |
| 436 | } else { |
| 437 | for (uint32_t u = 0; u < set->count; ++u) { |
| 438 | struct lyd_node *node = (struct lyd_node *)set->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 439 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 440 | printf(" %s \"%s\"", lys_nodetype2str(node->schema->nodetype), node->schema->name); |
| 441 | if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 442 | printf(" (value: \"%s\")\n", lyd_get_value(node)); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 443 | } else if (node->schema->nodetype == LYS_LIST) { |
| 444 | printf(" ("); |
| 445 | for (struct lyd_node *key = ((struct lyd_node_inner *)node)->child; key && lysc_is_key(key->schema); key = key->next) { |
| 446 | printf("%s\"%s\": \"%s\";", (key != ((struct lyd_node_inner *)node)->child) ? " " : "", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 447 | key->schema->name, lyd_get_value(key)); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 448 | } |
| 449 | printf(")\n"); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | ly_set_free(set, NULL); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 459 | process_data(struct ly_ctx *ctx, enum lyd_type data_type, uint8_t merge, LYD_FORMAT format, struct ly_out *out, |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 460 | uint32_t options_parse, uint32_t options_validate, uint32_t options_print, struct cmdline_file *operational_f, |
| 461 | struct cmdline_file *rpc_f, struct ly_set *inputs, struct ly_set *xpaths) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 462 | { |
Radek Krejci | 89757c1 | 2020-11-26 12:07:47 +0100 | [diff] [blame] | 463 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 464 | struct lyd_node *tree = NULL, *op = NULL, *envp = NULL, *merged_tree = NULL, *oper_tree = NULL; |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 465 | char *path = NULL; |
| 466 | struct ly_set *set = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 467 | |
| 468 | /* additional operational datastore */ |
| 469 | if (operational_f && operational_f->in) { |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 470 | ret = lyd_parse_data(ctx, NULL, operational_f->in, operational_f->format, LYD_PARSE_ONLY, 0, &oper_tree); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 471 | if (ret) { |
| 472 | YLMSG_E("Failed to parse operational datastore file \"%s\".\n", operational_f->path); |
| 473 | goto cleanup; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | for (uint32_t u = 0; u < inputs->count; ++u) { |
| 478 | struct cmdline_file *input_f = (struct cmdline_file *)inputs->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 479 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 480 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 481 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 482 | ret = lyd_parse_data(ctx, NULL, input_f->in, input_f->format, options_parse, options_validate, &tree); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 483 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 484 | case LYD_TYPE_RPC_YANG: |
| 485 | case LYD_TYPE_REPLY_YANG: |
| 486 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 487 | ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, data_type, &tree, &op); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 488 | break; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 489 | case LYD_TYPE_RPC_NETCONF: |
| 490 | case LYD_TYPE_NOTIF_NETCONF: |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 491 | ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, data_type, &envp, &op); |
| 492 | |
| 493 | /* adjust pointers */ |
| 494 | for (tree = op; lyd_parent(tree); tree = lyd_parent(tree)) {} |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 495 | break; |
| 496 | case LYD_TYPE_REPLY_NETCONF: |
| 497 | /* parse source RPC operation */ |
| 498 | assert(rpc_f && rpc_f->in); |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 499 | ret = lyd_parse_op(ctx, NULL, rpc_f->in, rpc_f->format, LYD_TYPE_RPC_NETCONF, &envp, &op); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 500 | if (ret) { |
| 501 | YLMSG_E("Failed to parse source NETCONF RPC operation file \"%s\".\n", rpc_f->path); |
| 502 | goto cleanup; |
| 503 | } |
| 504 | |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 505 | /* adjust pointers */ |
| 506 | for (tree = op; lyd_parent(tree); tree = lyd_parent(tree)) {} |
| 507 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 508 | /* free input */ |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 509 | lyd_free_siblings(lyd_child(op)); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 510 | |
| 511 | /* we do not care */ |
| 512 | lyd_free_all(envp); |
| 513 | envp = NULL; |
| 514 | |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 515 | ret = lyd_parse_op(ctx, op, input_f->in, input_f->format, data_type, &envp, NULL); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 516 | break; |
Radek Krejci | 89757c1 | 2020-11-26 12:07:47 +0100 | [diff] [blame] | 517 | default: |
| 518 | YLMSG_E("Internal error (%s:%d).\n", __FILE__, __LINE__); |
| 519 | goto cleanup; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (ret) { |
| 523 | YLMSG_E("Failed to parse input data file \"%s\".\n", input_f->path); |
| 524 | goto cleanup; |
| 525 | } |
| 526 | |
| 527 | if (merge) { |
| 528 | /* merge the data so far parsed for later validation and print */ |
| 529 | if (!merged_tree) { |
| 530 | merged_tree = tree; |
| 531 | } else { |
| 532 | ret = lyd_merge_siblings(&merged_tree, tree, LYD_MERGE_DESTRUCT); |
| 533 | if (ret) { |
| 534 | YLMSG_E("Merging %s with previous data failed.\n", input_f->path); |
| 535 | goto cleanup; |
| 536 | } |
| 537 | } |
| 538 | tree = NULL; |
| 539 | } else if (format) { |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 540 | /* print */ |
| 541 | switch (data_type) { |
| 542 | case LYD_TYPE_DATA_YANG: |
| 543 | lyd_print_all(out, tree, format, options_print); |
| 544 | break; |
| 545 | case LYD_TYPE_RPC_YANG: |
| 546 | case LYD_TYPE_REPLY_YANG: |
| 547 | case LYD_TYPE_NOTIF_YANG: |
| 548 | case LYD_TYPE_RPC_NETCONF: |
| 549 | case LYD_TYPE_NOTIF_NETCONF: |
| 550 | lyd_print_tree(out, tree, format, options_print); |
| 551 | break; |
| 552 | case LYD_TYPE_REPLY_NETCONF: |
| 553 | /* just the output */ |
| 554 | lyd_print_tree(out, lyd_child(tree), format, options_print); |
| 555 | break; |
| 556 | default: |
| 557 | assert(0); |
| 558 | } |
Michal Vasko | 0bec322 | 2022-04-22 07:46:00 +0200 | [diff] [blame] | 559 | } else { |
| 560 | /* validation of the RPC/Action/reply/Notification with the operational datastore, if any */ |
| 561 | switch (data_type) { |
| 562 | case LYD_TYPE_DATA_YANG: |
| 563 | /* already validated */ |
| 564 | break; |
| 565 | case LYD_TYPE_RPC_YANG: |
| 566 | case LYD_TYPE_RPC_NETCONF: |
| 567 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_RPC_YANG, NULL); |
| 568 | break; |
| 569 | case LYD_TYPE_REPLY_YANG: |
| 570 | case LYD_TYPE_REPLY_NETCONF: |
| 571 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_REPLY_YANG, NULL); |
| 572 | break; |
| 573 | case LYD_TYPE_NOTIF_YANG: |
| 574 | case LYD_TYPE_NOTIF_NETCONF: |
| 575 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_NOTIF_YANG, NULL); |
| 576 | break; |
| 577 | default: |
| 578 | assert(0); |
| 579 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 580 | if (ret) { |
Michal Vasko | 0bec322 | 2022-04-22 07:46:00 +0200 | [diff] [blame] | 581 | if (operational_f->path) { |
| 582 | YLMSG_E("Failed to validate input data file \"%s\" with operational datastore \"%s\".\n", |
| 583 | input_f->path, operational_f->path); |
| 584 | } else { |
| 585 | YLMSG_E("Failed to validate input data file \"%s\".\n", input_f->path); |
| 586 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 587 | goto cleanup; |
| 588 | } |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 589 | |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 590 | if (op && oper_tree && lyd_parent(op)) { |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 591 | /* check operation parent existence */ |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 592 | path = lyd_path(lyd_parent(op), LYD_PATH_STD, NULL, 0); |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 593 | if (!path) { |
| 594 | ret = LY_EMEM; |
| 595 | goto cleanup; |
| 596 | } |
| 597 | if ((ret = lyd_find_xpath(oper_tree, path, &set))) { |
| 598 | goto cleanup; |
| 599 | } |
| 600 | if (!set->count) { |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 601 | YLMSG_E("Operation \"%s\" parent \"%s\" not found in the operational data.\n", LYD_NAME(op), path); |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 602 | ret = LY_EVALID; |
| 603 | goto cleanup; |
| 604 | } |
| 605 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 606 | } |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 607 | |
| 608 | /* next iter */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 609 | lyd_free_all(tree); |
| 610 | tree = NULL; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 611 | lyd_free_all(envp); |
| 612 | envp = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (merge) { |
| 616 | /* validate the merged result */ |
| 617 | ret = lyd_validate_all(&merged_tree, ctx, LYD_VALIDATE_PRESENT, NULL); |
| 618 | if (ret) { |
| 619 | YLMSG_E("Merged data are not valid.\n"); |
| 620 | goto cleanup; |
| 621 | } |
| 622 | |
| 623 | if (format) { |
| 624 | /* and print it */ |
| 625 | lyd_print_all(out, merged_tree, format, options_print); |
| 626 | } |
| 627 | |
Michal Vasko | f8e71f7 | 2022-03-29 12:12:58 +0200 | [diff] [blame] | 628 | for (uint32_t u = 0; xpaths && (u < xpaths->count); ++u) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 629 | if (evaluate_xpath(merged_tree, (const char *)xpaths->objs[u])) { |
| 630 | goto cleanup; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | cleanup: |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 636 | lyd_free_all(tree); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 637 | lyd_free_all(envp); |
Michal Vasko | e1f495b | 2022-04-20 08:32:41 +0200 | [diff] [blame] | 638 | lyd_free_all(merged_tree); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 639 | lyd_free_all(oper_tree); |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 640 | free(path); |
| 641 | ly_set_free(set, NULL); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 642 | return ret; |
| 643 | } |