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 | |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 212 | int |
| 213 | collect_features(const struct lys_module *mod, struct ly_set *set) |
| 214 | { |
| 215 | struct lysp_feature *f = NULL; |
| 216 | uint32_t idx = 0; |
| 217 | |
| 218 | while ((f = lysp_feature_next(f, mod->parsed, &idx))) { |
| 219 | if (ly_set_add(set, (void *)f->name, 1, NULL)) { |
| 220 | YLMSG_E("Memory allocation failed.\n"); |
| 221 | ly_set_erase(set, NULL); |
| 222 | return 1; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | print_features(struct ly_out *out, const struct lys_module *mod, const struct ly_set *set) |
| 231 | { |
| 232 | size_t max_len; |
| 233 | uint32_t j; |
| 234 | const char *name; |
| 235 | |
| 236 | /* header */ |
| 237 | ly_print(out, "%s:\n", mod->name); |
| 238 | |
| 239 | /* no features */ |
| 240 | if (!set->count) { |
| 241 | ly_print(out, "\t(none)\n"); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | /* get max len, so the statuses of all the features will be aligned */ |
| 246 | max_len = 0; |
| 247 | for (j = 0; j < set->count; ++j) { |
| 248 | name = set->objs[j]; |
| 249 | if (strlen(name) > max_len) { |
| 250 | max_len = strlen(name); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* print features */ |
| 255 | for (j = 0; j < set->count; ++j) { |
| 256 | name = set->objs[j]; |
| 257 | ly_print(out, "\t%-*s (%s)\n", (int)max_len, name, lys_feature_value(mod, name) ? "off" : "on"); |
| 258 | } |
| 259 | |
| 260 | ly_print(out, "\n"); |
| 261 | } |
| 262 | |
| 263 | int |
| 264 | generate_features_output(const struct lys_module *mod, const struct ly_set *set, char **features_param) |
| 265 | { |
| 266 | uint32_t j; |
| 267 | /* |
| 268 | * features_len - length of all the features in the current module |
| 269 | * added_len - length of a string to be added, = features_len + extra necessary length |
| 270 | * param_len - length of the parameter before appending new string |
| 271 | */ |
| 272 | size_t features_len, added_len, param_len; |
| 273 | char *tmp; |
| 274 | |
| 275 | features_len = 0; |
| 276 | for (j = 0; j < set->count; j++) { |
| 277 | features_len += strlen(set->objs[j]); |
| 278 | } |
| 279 | |
| 280 | if (j == 0) { |
| 281 | /* no features */ |
| 282 | added_len = strlen("-F ") + strlen(mod->name) + strlen(":"); |
| 283 | } else { |
| 284 | /* j = comma count, -1 because of trailing comma */ |
| 285 | added_len = strlen("-F ") + strlen(mod->name) + strlen(":") + features_len + j - 1; |
| 286 | } |
| 287 | |
| 288 | /* to avoid strlen(NULL) if this is the first call */ |
| 289 | param_len = 0; |
| 290 | if (*features_param) { |
| 291 | param_len = strlen(*features_param); |
| 292 | } |
| 293 | |
| 294 | /* +1 because of white space at the beginning */ |
| 295 | tmp = realloc(*features_param, param_len + added_len + 1 + 1); |
| 296 | if (!tmp) { |
| 297 | goto error; |
| 298 | } else { |
| 299 | *features_param = tmp; |
| 300 | } |
| 301 | sprintf(*features_param + param_len, " -F %s:", mod->name); |
| 302 | |
| 303 | for (j = 0; j < set->count; j++) { |
| 304 | strcat(*features_param, set->objs[j]); |
| 305 | /* no trailing comma */ |
| 306 | if (j != (set->count - 1)) { |
| 307 | strcat(*features_param, ","); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | |
| 313 | error: |
| 314 | YLMSG_E("Memory allocation failed (%s:%d, %s).\n", __FILE__, __LINE__, strerror(errno)); |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | int |
| 319 | print_all_features(struct ly_out *out, const struct ly_ctx *ctx, ly_bool generate_features, char **features_param) |
| 320 | { |
| 321 | int ret = 0; |
| 322 | uint32_t i = 0; |
| 323 | struct lys_module *mod; |
| 324 | struct ly_set set = {0}; |
| 325 | |
| 326 | while ((mod = ly_ctx_get_module_iter(ctx, &i)) != NULL) { |
| 327 | /* only care about implemented modules */ |
| 328 | if (!mod->implemented) { |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | /* always erase the set, so the previous module's features don't carry over to the next module's features */ |
| 333 | ly_set_erase(&set, NULL); |
| 334 | |
| 335 | if (collect_features(mod, &set)) { |
| 336 | ret = 1; |
| 337 | goto cleanup; |
| 338 | } |
| 339 | |
| 340 | if (generate_features && generate_features_output(mod, &set, features_param)) { |
| 341 | ret = 1; |
| 342 | goto cleanup; |
| 343 | } |
| 344 | print_features(out, mod, &set); |
| 345 | } |
| 346 | |
| 347 | cleanup: |
| 348 | ly_set_erase(&set, NULL); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 352 | void |
| 353 | free_cmdline_file(void *cmdline_file) |
| 354 | { |
| 355 | struct cmdline_file *rec = (struct cmdline_file *)cmdline_file; |
| 356 | |
| 357 | if (rec) { |
| 358 | ly_in_free(rec->in, 1); |
| 359 | free(rec); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | free_cmdline(char *argv[]) |
| 365 | { |
| 366 | if (argv) { |
| 367 | free(argv[0]); |
| 368 | free(argv); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | int |
| 373 | parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[]) |
| 374 | { |
| 375 | int count; |
| 376 | char **vector; |
| 377 | char *ptr; |
| 378 | char qmark = 0; |
| 379 | |
| 380 | assert(cmdline); |
| 381 | assert(argc_p); |
| 382 | assert(argv_p); |
| 383 | |
| 384 | /* init */ |
| 385 | optind = 0; /* reinitialize getopt() */ |
| 386 | count = 1; |
| 387 | vector = malloc((count + 1) * sizeof *vector); |
| 388 | vector[0] = strdup(cmdline); |
| 389 | |
| 390 | /* command name */ |
| 391 | strtok(vector[0], " "); |
| 392 | |
| 393 | /* arguments */ |
| 394 | while ((ptr = strtok(NULL, " "))) { |
| 395 | size_t len; |
| 396 | void *r; |
| 397 | |
| 398 | len = strlen(ptr); |
| 399 | |
| 400 | if (qmark) { |
| 401 | /* still in quotated text */ |
| 402 | /* remove NULL termination of the previous token since it is not a token, |
| 403 | * but a part of the quotation string */ |
| 404 | ptr[-1] = ' '; |
| 405 | |
| 406 | if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) { |
| 407 | /* end of quotation */ |
| 408 | qmark = 0; |
| 409 | /* shorten the argument by the terminating quotation mark */ |
| 410 | ptr[len - 1] = '\0'; |
| 411 | } |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | /* another token in cmdline */ |
| 416 | ++count; |
| 417 | r = realloc(vector, (count + 1) * sizeof *vector); |
| 418 | if (!r) { |
Michal Vasko | 9a5f3dd | 2021-11-23 08:59:53 +0100 | [diff] [blame] | 419 | 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] | 420 | free(vector); |
| 421 | return -1; |
| 422 | } |
| 423 | vector = r; |
| 424 | vector[count - 1] = ptr; |
| 425 | |
| 426 | if ((ptr[0] == '"') || (ptr[0] == '\'')) { |
| 427 | /* remember the quotation mark to identify end of quotation */ |
| 428 | qmark = ptr[0]; |
| 429 | |
| 430 | /* move the remembered argument after the quotation mark */ |
| 431 | ++vector[count - 1]; |
| 432 | |
| 433 | /* check if the quotation is terminated within this token */ |
| 434 | if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) { |
| 435 | /* end of quotation */ |
| 436 | qmark = 0; |
| 437 | /* shorten the argument by the terminating quotation mark */ |
| 438 | ptr[len - 1] = '\0'; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | vector[count] = NULL; |
| 443 | |
| 444 | *argc_p = count; |
| 445 | *argv_p = vector; |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int |
| 451 | get_format(const char *filename, LYS_INFORMAT *schema, LYD_FORMAT *data) |
| 452 | { |
| 453 | char *ptr; |
| 454 | LYS_INFORMAT informat_s; |
| 455 | LYD_FORMAT informat_d; |
| 456 | |
| 457 | /* get the file format */ |
| 458 | if ((ptr = strrchr(filename, '.')) != NULL) { |
| 459 | ++ptr; |
| 460 | if (!strcmp(ptr, "yang")) { |
| 461 | informat_s = LYS_IN_YANG; |
| 462 | informat_d = 0; |
| 463 | } else if (!strcmp(ptr, "yin")) { |
| 464 | informat_s = LYS_IN_YIN; |
| 465 | informat_d = 0; |
| 466 | } else if (!strcmp(ptr, "xml")) { |
| 467 | informat_s = 0; |
| 468 | informat_d = LYD_XML; |
| 469 | } else if (!strcmp(ptr, "json")) { |
| 470 | informat_s = 0; |
| 471 | informat_d = LYD_JSON; |
Michal Vasko | d3b1054 | 2021-02-03 11:31:16 +0100 | [diff] [blame] | 472 | } else if (!strcmp(ptr, "lyb")) { |
| 473 | informat_s = 0; |
| 474 | informat_d = LYD_LYB; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 475 | } else { |
| 476 | YLMSG_E("Input file \"%s\" in an unknown format \"%s\".\n", filename, ptr); |
| 477 | return 0; |
| 478 | } |
| 479 | } else { |
| 480 | YLMSG_E("Input file \"%s\" without file extension - unknown format.\n", filename); |
| 481 | return 1; |
| 482 | } |
| 483 | |
| 484 | if (informat_d) { |
| 485 | if (!data) { |
| 486 | YLMSG_E("Input file \"%s\" not expected to contain data instances (unexpected format).\n", filename); |
| 487 | return 2; |
| 488 | } |
| 489 | (*data) = informat_d; |
| 490 | } else if (informat_s) { |
| 491 | if (!schema) { |
| 492 | YLMSG_E("Input file \"%s\" not expected to contain schema definition (unexpected format).\n", filename); |
| 493 | return 3; |
| 494 | } |
| 495 | (*schema) = informat_s; |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | int |
| 502 | print_list(struct ly_out *out, struct ly_ctx *ctx, LYD_FORMAT outformat) |
| 503 | { |
| 504 | struct lyd_node *ylib; |
| 505 | uint32_t idx = 0, has_modules = 0; |
| 506 | const struct lys_module *mod; |
| 507 | |
| 508 | if (outformat != LYD_UNKNOWN) { |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 509 | 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] | 510 | 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] | 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | lyd_print_all(out, ylib, outformat, 0); |
| 515 | lyd_free_all(ylib); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* iterate schemas in context and provide just the basic info */ |
| 520 | ly_print(out, "List of the loaded models:\n"); |
| 521 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
| 522 | has_modules++; |
| 523 | |
| 524 | /* conformance print */ |
| 525 | if (mod->implemented) { |
| 526 | ly_print(out, " I"); |
| 527 | } else { |
| 528 | ly_print(out, " i"); |
| 529 | } |
| 530 | |
| 531 | /* module print */ |
| 532 | ly_print(out, " %s", mod->name); |
| 533 | if (mod->revision) { |
| 534 | ly_print(out, "@%s", mod->revision); |
| 535 | } |
| 536 | |
| 537 | /* submodules print */ |
| 538 | if (mod->parsed && mod->parsed->includes) { |
| 539 | uint64_t u = 0; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 540 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 541 | ly_print(out, " ("); |
| 542 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 543 | ly_print(out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name); |
| 544 | if (mod->parsed->includes[u].rev[0]) { |
| 545 | ly_print(out, "@%s", mod->parsed->includes[u].rev); |
| 546 | } |
| 547 | } |
| 548 | ly_print(out, ")"); |
| 549 | } |
| 550 | |
| 551 | /* finish the line */ |
| 552 | ly_print(out, "\n"); |
| 553 | } |
| 554 | |
| 555 | if (!has_modules) { |
| 556 | ly_print(out, "\t(none)\n"); |
| 557 | } |
| 558 | |
| 559 | ly_print_flush(out); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | int |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 564 | evaluate_xpath(const struct lyd_node *tree, const char *xpath) |
| 565 | { |
Radek Krejci | 89757c1 | 2020-11-26 12:07:47 +0100 | [diff] [blame] | 566 | struct ly_set *set = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 567 | |
| 568 | if (lyd_find_xpath(tree, xpath, &set)) { |
| 569 | return -1; |
| 570 | } |
| 571 | |
| 572 | /* print result */ |
| 573 | printf("XPath \"%s\" evaluation result:\n", xpath); |
| 574 | if (!set->count) { |
| 575 | printf("\tEmpty\n"); |
| 576 | } else { |
| 577 | for (uint32_t u = 0; u < set->count; ++u) { |
| 578 | struct lyd_node *node = (struct lyd_node *)set->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 579 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 580 | printf(" %s \"%s\"", lys_nodetype2str(node->schema->nodetype), node->schema->name); |
| 581 | if (node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 582 | printf(" (value: \"%s\")\n", lyd_get_value(node)); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 583 | } else if (node->schema->nodetype == LYS_LIST) { |
| 584 | printf(" ("); |
| 585 | for (struct lyd_node *key = ((struct lyd_node_inner *)node)->child; key && lysc_is_key(key->schema); key = key->next) { |
| 586 | printf("%s\"%s\": \"%s\";", (key != ((struct lyd_node_inner *)node)->child) ? " " : "", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 587 | key->schema->name, lyd_get_value(key)); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 588 | } |
| 589 | printf(")\n"); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | ly_set_free(set, NULL); |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 599 | 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] | 600 | uint32_t options_parse, uint32_t options_validate, uint32_t options_print, struct cmdline_file *operational_f, |
| 601 | 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] | 602 | { |
Radek Krejci | 89757c1 | 2020-11-26 12:07:47 +0100 | [diff] [blame] | 603 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 604 | 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] | 605 | char *path = NULL; |
| 606 | struct ly_set *set = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 607 | |
| 608 | /* additional operational datastore */ |
| 609 | if (operational_f && operational_f->in) { |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 610 | 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] | 611 | if (ret) { |
| 612 | YLMSG_E("Failed to parse operational datastore file \"%s\".\n", operational_f->path); |
| 613 | goto cleanup; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | for (uint32_t u = 0; u < inputs->count; ++u) { |
| 618 | struct cmdline_file *input_f = (struct cmdline_file *)inputs->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 619 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 620 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 621 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 622 | 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] | 623 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 624 | case LYD_TYPE_RPC_YANG: |
| 625 | case LYD_TYPE_REPLY_YANG: |
| 626 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 627 | 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] | 628 | break; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 629 | case LYD_TYPE_RPC_NETCONF: |
| 630 | case LYD_TYPE_NOTIF_NETCONF: |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 631 | ret = lyd_parse_op(ctx, NULL, input_f->in, input_f->format, data_type, &envp, &op); |
| 632 | |
| 633 | /* adjust pointers */ |
| 634 | for (tree = op; lyd_parent(tree); tree = lyd_parent(tree)) {} |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 635 | break; |
| 636 | case LYD_TYPE_REPLY_NETCONF: |
| 637 | /* parse source RPC operation */ |
| 638 | assert(rpc_f && rpc_f->in); |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 639 | 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] | 640 | if (ret) { |
| 641 | YLMSG_E("Failed to parse source NETCONF RPC operation file \"%s\".\n", rpc_f->path); |
| 642 | goto cleanup; |
| 643 | } |
| 644 | |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 645 | /* adjust pointers */ |
| 646 | for (tree = op; lyd_parent(tree); tree = lyd_parent(tree)) {} |
| 647 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 648 | /* free input */ |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 649 | lyd_free_siblings(lyd_child(op)); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 650 | |
| 651 | /* we do not care */ |
| 652 | lyd_free_all(envp); |
| 653 | envp = NULL; |
| 654 | |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 655 | 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] | 656 | break; |
Radek Krejci | 89757c1 | 2020-11-26 12:07:47 +0100 | [diff] [blame] | 657 | default: |
| 658 | YLMSG_E("Internal error (%s:%d).\n", __FILE__, __LINE__); |
| 659 | goto cleanup; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | if (ret) { |
| 663 | YLMSG_E("Failed to parse input data file \"%s\".\n", input_f->path); |
| 664 | goto cleanup; |
| 665 | } |
| 666 | |
| 667 | if (merge) { |
| 668 | /* merge the data so far parsed for later validation and print */ |
| 669 | if (!merged_tree) { |
| 670 | merged_tree = tree; |
| 671 | } else { |
| 672 | ret = lyd_merge_siblings(&merged_tree, tree, LYD_MERGE_DESTRUCT); |
| 673 | if (ret) { |
| 674 | YLMSG_E("Merging %s with previous data failed.\n", input_f->path); |
| 675 | goto cleanup; |
| 676 | } |
| 677 | } |
| 678 | tree = NULL; |
| 679 | } else if (format) { |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 680 | /* print */ |
| 681 | switch (data_type) { |
| 682 | case LYD_TYPE_DATA_YANG: |
| 683 | lyd_print_all(out, tree, format, options_print); |
| 684 | break; |
| 685 | case LYD_TYPE_RPC_YANG: |
| 686 | case LYD_TYPE_REPLY_YANG: |
| 687 | case LYD_TYPE_NOTIF_YANG: |
| 688 | case LYD_TYPE_RPC_NETCONF: |
| 689 | case LYD_TYPE_NOTIF_NETCONF: |
| 690 | lyd_print_tree(out, tree, format, options_print); |
| 691 | break; |
| 692 | case LYD_TYPE_REPLY_NETCONF: |
| 693 | /* just the output */ |
| 694 | lyd_print_tree(out, lyd_child(tree), format, options_print); |
| 695 | break; |
| 696 | default: |
| 697 | assert(0); |
| 698 | } |
Michal Vasko | 0bec322 | 2022-04-22 07:46:00 +0200 | [diff] [blame] | 699 | } else { |
| 700 | /* validation of the RPC/Action/reply/Notification with the operational datastore, if any */ |
| 701 | switch (data_type) { |
| 702 | case LYD_TYPE_DATA_YANG: |
| 703 | /* already validated */ |
| 704 | break; |
| 705 | case LYD_TYPE_RPC_YANG: |
| 706 | case LYD_TYPE_RPC_NETCONF: |
| 707 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_RPC_YANG, NULL); |
| 708 | break; |
| 709 | case LYD_TYPE_REPLY_YANG: |
| 710 | case LYD_TYPE_REPLY_NETCONF: |
| 711 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_REPLY_YANG, NULL); |
| 712 | break; |
| 713 | case LYD_TYPE_NOTIF_YANG: |
| 714 | case LYD_TYPE_NOTIF_NETCONF: |
| 715 | ret = lyd_validate_op(tree, oper_tree, LYD_TYPE_NOTIF_YANG, NULL); |
| 716 | break; |
| 717 | default: |
| 718 | assert(0); |
| 719 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 720 | if (ret) { |
Michal Vasko | 0bec322 | 2022-04-22 07:46:00 +0200 | [diff] [blame] | 721 | if (operational_f->path) { |
| 722 | YLMSG_E("Failed to validate input data file \"%s\" with operational datastore \"%s\".\n", |
| 723 | input_f->path, operational_f->path); |
| 724 | } else { |
| 725 | YLMSG_E("Failed to validate input data file \"%s\".\n", input_f->path); |
| 726 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 727 | goto cleanup; |
| 728 | } |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 729 | |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 730 | if (op && oper_tree && lyd_parent(op)) { |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 731 | /* check operation parent existence */ |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 732 | path = lyd_path(lyd_parent(op), LYD_PATH_STD, NULL, 0); |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 733 | if (!path) { |
| 734 | ret = LY_EMEM; |
| 735 | goto cleanup; |
| 736 | } |
| 737 | if ((ret = lyd_find_xpath(oper_tree, path, &set))) { |
| 738 | goto cleanup; |
| 739 | } |
| 740 | if (!set->count) { |
Michal Vasko | ff141b0 | 2022-05-02 09:22:36 +0200 | [diff] [blame] | 741 | 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] | 742 | ret = LY_EVALID; |
| 743 | goto cleanup; |
| 744 | } |
| 745 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 746 | } |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 747 | |
| 748 | /* next iter */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 749 | lyd_free_all(tree); |
| 750 | tree = NULL; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 751 | lyd_free_all(envp); |
| 752 | envp = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | if (merge) { |
| 756 | /* validate the merged result */ |
| 757 | ret = lyd_validate_all(&merged_tree, ctx, LYD_VALIDATE_PRESENT, NULL); |
| 758 | if (ret) { |
| 759 | YLMSG_E("Merged data are not valid.\n"); |
| 760 | goto cleanup; |
| 761 | } |
| 762 | |
| 763 | if (format) { |
| 764 | /* and print it */ |
| 765 | lyd_print_all(out, merged_tree, format, options_print); |
| 766 | } |
| 767 | |
Michal Vasko | f8e71f7 | 2022-03-29 12:12:58 +0200 | [diff] [blame] | 768 | for (uint32_t u = 0; xpaths && (u < xpaths->count); ++u) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 769 | if (evaluate_xpath(merged_tree, (const char *)xpaths->objs[u])) { |
| 770 | goto cleanup; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | cleanup: |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 776 | lyd_free_all(tree); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 777 | lyd_free_all(envp); |
Michal Vasko | e1f495b | 2022-04-20 08:32:41 +0200 | [diff] [blame] | 778 | lyd_free_all(merged_tree); |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 779 | lyd_free_all(oper_tree); |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 780 | free(path); |
| 781 | ly_set_free(set, NULL); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 782 | return ret; |
| 783 | } |