Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file common.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 4 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 5 | * @brief libyang's yanglint tool - common functions for both interactive and non-interactive mode. |
| 6 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 17 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 18 | |
| 19 | #include "common.h" |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <errno.h> |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 23 | #include <stdarg.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
| 29 | #include "compat.h" |
| 30 | #include "libyang.h" |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 31 | #include "plugins_exts.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 32 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 33 | |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 34 | void |
| 35 | yl_log(ly_bool err, const char *format, ...) |
| 36 | { |
| 37 | char msg[256]; |
| 38 | va_list ap; |
| 39 | |
| 40 | va_start(ap, format); |
| 41 | vsnprintf(msg, 256, format, ap); |
| 42 | va_end(ap); |
| 43 | |
| 44 | fprintf(stderr, "YANGLINT[%c]: %s\n", err ? 'E' : 'W', msg); |
| 45 | } |
| 46 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 47 | int |
| 48 | parse_schema_path(const char *path, char **dir, char **module) |
| 49 | { |
| 50 | char *p; |
| 51 | |
| 52 | assert(dir); |
| 53 | assert(module); |
| 54 | |
| 55 | /* split the path to dirname and basename for further work */ |
| 56 | *dir = strdup(path); |
| 57 | *module = strrchr(*dir, '/'); |
| 58 | if (!(*module)) { |
| 59 | *module = *dir; |
| 60 | *dir = strdup("./"); |
| 61 | } else { |
| 62 | *module[0] = '\0'; /* break the dir */ |
| 63 | *module = strdup((*module) + 1); |
| 64 | } |
| 65 | /* get the pure module name without suffix or revision part of the filename */ |
| 66 | if ((p = strchr(*module, '@'))) { |
| 67 | /* revision */ |
| 68 | *p = '\0'; |
| 69 | } else if ((p = strrchr(*module, '.'))) { |
| 70 | /* fileformat suffix */ |
| 71 | *p = '\0'; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in) |
| 79 | { |
| 80 | struct stat st; |
| 81 | |
| 82 | /* check that the filepath exists and is a regular file */ |
| 83 | if (stat(filepath, &st) == -1) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 84 | YLMSG_E("Unable to use input filepath (%s) - %s.", filepath, strerror(errno)); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | if (!S_ISREG(st.st_mode)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 88 | YLMSG_E("Provided input file (%s) is not a regular file.", filepath); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 92 | if (get_format(filepath, format_schema, format_data)) { |
| 93 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 94 | } |
| 95 | |
aPiecek | 2457b5e | 2023-06-12 11:40:14 +0200 | [diff] [blame] | 96 | if (in && ly_in_new_filepath(filepath, 0, in)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 97 | YLMSG_E("Unable to process input file."); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 104 | LYS_INFORMAT |
| 105 | get_schema_format(const char *filename) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 106 | { |
| 107 | char *ptr; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 108 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 109 | if ((ptr = strrchr(filename, '.')) != NULL) { |
| 110 | ++ptr; |
| 111 | if (!strcmp(ptr, "yang")) { |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 112 | return LYS_IN_YANG; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 113 | } else if (!strcmp(ptr, "yin")) { |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 114 | return LYS_IN_YIN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 115 | } else { |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 116 | return LYS_IN_UNKNOWN; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 117 | } |
| 118 | } else { |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 119 | return LYS_IN_UNKNOWN; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | LYD_FORMAT |
| 124 | get_data_format(const char *filename) |
| 125 | { |
| 126 | char *ptr; |
| 127 | |
| 128 | if ((ptr = strrchr(filename, '.')) != NULL) { |
| 129 | ++ptr; |
| 130 | if (!strcmp(ptr, "xml")) { |
| 131 | return LYD_XML; |
| 132 | } else if (!strcmp(ptr, "json")) { |
| 133 | return LYD_JSON; |
| 134 | } else if (!strcmp(ptr, "lyb")) { |
| 135 | return LYD_LYB; |
| 136 | } else { |
| 137 | return LYD_UNKNOWN; |
| 138 | } |
| 139 | } else { |
| 140 | return LYD_UNKNOWN; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | get_format(const char *filepath, LYS_INFORMAT *schema_form, LYD_FORMAT *data_form) |
| 146 | { |
| 147 | LYS_INFORMAT schema; |
| 148 | LYD_FORMAT data; |
| 149 | |
| 150 | schema = !schema_form || !*schema_form ? LYS_IN_UNKNOWN : *schema_form; |
| 151 | data = !data_form || !*data_form ? LYD_UNKNOWN : *data_form; |
| 152 | |
| 153 | if (!schema) { |
| 154 | schema = get_schema_format(filepath); |
| 155 | } |
| 156 | if (!data) { |
| 157 | data = get_data_format(filepath); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 158 | } |
| 159 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 160 | if (!schema && !data) { |
| 161 | YLMSG_E("Input schema format for %s file not recognized.", filepath); |
| 162 | return -1; |
| 163 | } else if (!data && !schema) { |
| 164 | YLMSG_E("Input data format for %s file not recognized.", filepath); |
| 165 | return -1; |
| 166 | } |
| 167 | assert(schema || data); |
| 168 | |
| 169 | if (schema_form) { |
| 170 | *schema_form = schema; |
| 171 | } |
| 172 | if (data_form) { |
| 173 | *data_form = data; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 179 | const struct lysc_node * |
| 180 | find_schema_path(const struct ly_ctx *ctx, const char *schema_path) |
| 181 | { |
| 182 | const char *end, *module_name_end; |
| 183 | char *module_name = NULL; |
Michal Vasko | b10c93b | 2022-12-14 12:16:27 +0100 | [diff] [blame] | 184 | const struct lysc_node *node = NULL, *parent_node = NULL, *parent_node_tmp = NULL; |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 185 | const struct lys_module *module; |
| 186 | size_t node_name_len; |
| 187 | ly_bool found_exact_match = 0; |
| 188 | |
| 189 | /* iterate over each '/' in the path */ |
| 190 | while (schema_path) { |
| 191 | /* example: schema_path = /listen/endpoint |
| 192 | * end == NULL for endpoint, end exists for listen */ |
| 193 | end = strchr(schema_path + 1, '/'); |
| 194 | if (end) { |
| 195 | node_name_len = end - schema_path - 1; |
| 196 | } else { |
| 197 | node_name_len = strlen(schema_path + 1); |
| 198 | } |
| 199 | |
| 200 | /* ex: schema_path = /ietf-interfaces:interfaces/interface/ietf-ip:ipv4 */ |
| 201 | module_name_end = strchr(schema_path, ':'); |
roman | ba1421b | 2022-10-07 11:02:36 +0200 | [diff] [blame] | 202 | if (module_name_end && (!end || (module_name_end < end))) { |
| 203 | /* only get module's name, if it is in the current scope */ |
| 204 | free(module_name); |
| 205 | /* - 1 because module_name_end points to ':' */ |
| 206 | module_name = strndup(schema_path + 1, module_name_end - schema_path - 1); |
| 207 | if (!module_name) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 208 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
roman | ba1421b | 2022-10-07 11:02:36 +0200 | [diff] [blame] | 209 | parent_node = NULL; |
| 210 | goto cleanup; |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 211 | } |
roman | ba1421b | 2022-10-07 11:02:36 +0200 | [diff] [blame] | 212 | /* move the pointer to the beginning of the node's name - 1 */ |
| 213 | schema_path = module_name_end; |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 214 | |
roman | ba1421b | 2022-10-07 11:02:36 +0200 | [diff] [blame] | 215 | /* recalculate the length of the node's name, because the module prefix mustn't be compared later */ |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 216 | if (module_name_end < end) { |
roman | ba1421b | 2022-10-07 11:02:36 +0200 | [diff] [blame] | 217 | node_name_len = end - module_name_end - 1; |
| 218 | } else if (!end) { |
| 219 | node_name_len = strlen(module_name_end + 1); |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | module = ly_ctx_get_module_implemented(ctx, module_name); |
| 224 | if (!module) { |
| 225 | /* unknown module name */ |
| 226 | parent_node = NULL; |
| 227 | goto cleanup; |
| 228 | } |
| 229 | |
| 230 | /* iterate over the node's siblings / module's top level containers */ |
| 231 | while ((node = lys_getnext(node, parent_node, module->compiled, LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHCHOICE))) { |
| 232 | if (end && !strncmp(node->name, schema_path + 1, node_name_len) && (node->name[node_name_len] == '\0')) { |
| 233 | /* check if the whole node's name matches and it's not just a common prefix */ |
| 234 | parent_node = node; |
| 235 | break; |
| 236 | } else if (!strncmp(node->name, schema_path + 1, node_name_len)) { |
| 237 | /* do the same here, however if there is no exact match, use the last node with the same prefix */ |
| 238 | if (strlen(node->name) == node_name_len) { |
| 239 | parent_node = node; |
| 240 | found_exact_match = 1; |
| 241 | break; |
| 242 | } else { |
| 243 | parent_node_tmp = node; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (!end && !found_exact_match) { |
| 249 | /* no exact match */ |
| 250 | parent_node = parent_node_tmp; |
| 251 | } |
| 252 | found_exact_match = 0; |
| 253 | |
| 254 | /* next iter */ |
| 255 | schema_path = strchr(schema_path + 1, '/'); |
| 256 | } |
| 257 | |
| 258 | cleanup: |
| 259 | free(module_name); |
| 260 | return parent_node; |
| 261 | } |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 262 | |
| 263 | LY_ERR |
| 264 | ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free) |
| 265 | { |
| 266 | struct ly_ctx *ctx; |
| 267 | struct lyd_node *data = NULL; |
| 268 | |
| 269 | ctx = ext->module->ctx; |
| 270 | if (user_data) { |
| 271 | lyd_parse_data_path(ctx, user_data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &data); |
| 272 | } |
| 273 | |
| 274 | *ext_data = data; |
| 275 | *ext_data_free = 1; |
| 276 | return LY_SUCCESS; |
| 277 | } |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 278 | |
| 279 | LY_ERR |
| 280 | searchpath_strcat(char **searchpaths, const char *path) |
| 281 | { |
| 282 | uint64_t len; |
| 283 | char *new; |
| 284 | |
| 285 | if (!(*searchpaths)) { |
| 286 | *searchpaths = strdup(path); |
| 287 | return LY_SUCCESS; |
| 288 | } |
| 289 | |
| 290 | len = strlen(*searchpaths) + strlen(path) + strlen(PATH_SEPARATOR); |
| 291 | new = realloc(*searchpaths, sizeof(char) * len + 1); |
| 292 | if (!new) { |
| 293 | return LY_EMEM; |
| 294 | } |
| 295 | strcat(new, PATH_SEPARATOR); |
| 296 | strcat(new, path); |
| 297 | *searchpaths = new; |
| 298 | |
| 299 | return LY_SUCCESS; |
| 300 | } |