Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 4 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 5 | * @brief libyang's YANG Regular Expression tool |
| 6 | * |
| 7 | * Copyright (c) 2017 CESNET, z.s.p.o. |
| 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 | |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 16 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 17 | |
| 18 | #include <errno.h> |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 19 | #include <getopt.h> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 22 | #include <string.h> |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 23 | #include <sys/types.h> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "libyang.h" |
| 26 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 27 | #include "compat.h" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 28 | #include "tools/config.h" |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 29 | |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 30 | struct yr_pattern { |
| 31 | char *expr; |
| 32 | ly_bool invert; |
| 33 | }; |
| 34 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 35 | void |
| 36 | help(void) |
| 37 | { |
| 38 | fprintf(stdout, "YANG Regular Expressions processor.\n"); |
| 39 | fprintf(stdout, "Usage:\n"); |
| 40 | fprintf(stdout, " yangre [-hv]\n"); |
| 41 | fprintf(stdout, " yangre [-V] -p <regexp1> [-i] [-p <regexp2> [-i] ...] <string>\n"); |
| 42 | fprintf(stdout, " yangre [-V] -f <file>\n"); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 43 | fprintf(stdout, "Returns 0 if string matches the pattern(s) or if otherwise successful.\n"); |
| 44 | fprintf(stdout, "Returns 1 on error.\n"); |
| 45 | fprintf(stdout, "Returns 2 if string does not match the pattern(s).\n\n"); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 46 | fprintf(stdout, "Options:\n" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 47 | " -h, --help Show this help message and exit.\n" |
| 48 | " -v, --version Show version number and exit.\n" |
| 49 | " -V, --verbose Print the processing information.\n" |
| 50 | " -i, --invert-match Invert-match modifier for the closest preceding\n" |
| 51 | " pattern.\n" |
| 52 | " -p, --pattern=\"REGEXP\" Regular expression including the quoting,\n" |
| 53 | " which is applied the same way as in a YANG module.\n" |
| 54 | " -f, --file=\"FILE\" List of patterns and the <string> (separated by an\n" |
| 55 | " empty line) are taken from <file>. Invert-match is\n" |
| 56 | " indicated by the single space character at the \n" |
| 57 | " beginning of the pattern line. YANG quotation around\n" |
| 58 | " patterns is still expected, but that avoids issues with\n" |
| 59 | " reading quotation by shell. Avoid newline at the end\n" |
| 60 | " of the string line to represent empty <string>."); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 61 | fprintf(stdout, "Examples:\n" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 62 | " pattern \"[0-9a-fA-F]*\"; -> yangre -p '\"[0-9a-fA-F]*\"' '1F'\n" |
| 63 | " pattern '[a-zA-Z0-9\\-_.]*'; -> yangre -p \"'[a-zA-Z0-9\\-_.]*'\" 'a-b'\n" |
| 64 | " pattern [xX][mM][lL].*; -> yangre -p '[xX][mM][lL].*' 'xml-encoding'\n\n"); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 65 | fprintf(stdout, "Note that to pass YANG quoting through your shell, you are supposed to use\n" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 66 | "the other quotation around. For not-quoted patterns, use single quotes.\n\n"); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
| 70 | version(void) |
| 71 | { |
| 72 | fprintf(stdout, "yangre %s\n", PROJECT_VERSION); |
| 73 | } |
| 74 | |
| 75 | void |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 76 | pattern_error(LY_LOG_LEVEL level, const char *msg, const char *UNUSED(data_path), const char *UNUSED(schema_path), |
| 77 | uint64_t UNUSED(line)) |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 78 | { |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 79 | if (level == LY_LLERR) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 80 | fprintf(stderr, "yangre error: %s\n", msg); |
| 81 | } |
| 82 | } |
| 83 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 84 | static int |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 85 | add_pattern(struct yr_pattern **patterns, int *counter, char *pattern) |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 86 | { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 87 | void *reallocated; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 88 | int orig_counter; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 89 | |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 90 | /* Store the original number of items. */ |
| 91 | orig_counter = *counter; |
| 92 | |
| 93 | /* Reallocate 'patterns' memory with additional space. */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 94 | reallocated = realloc(*patterns, (orig_counter + 1) * sizeof **patterns); |
| 95 | if (!reallocated) { |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 96 | goto error; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 97 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 98 | (*patterns) = reallocated; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 99 | /* Allocated memory is now larger. */ |
| 100 | (*counter)++; |
| 101 | /* Copy the pattern and store it to the additonal space. */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 102 | (*patterns)[orig_counter].expr = strdup(pattern); |
| 103 | if (!(*patterns)[orig_counter].expr) { |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 104 | goto error; |
| 105 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 106 | (*patterns)[orig_counter].invert = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 107 | |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 108 | return 0; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 109 | |
| 110 | error: |
| 111 | fprintf(stderr, "yangre error: memory allocation error.\n"); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 112 | return 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 113 | } |
| 114 | |
aPiecek | ac2c282 | 2023-07-04 10:15:56 +0200 | [diff] [blame] | 115 | static int |
| 116 | create_empty_string(char **str) |
| 117 | { |
| 118 | free(*str); |
| 119 | *str = malloc(sizeof(char)); |
| 120 | if (!(*str)) { |
| 121 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 122 | return 1; |
| 123 | } |
| 124 | (*str)[0] = '\0'; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static ly_bool |
| 130 | file_is_empty(FILE *fp) |
| 131 | { |
| 132 | int c; |
| 133 | |
| 134 | c = fgetc(fp); |
| 135 | if (c == EOF) { |
| 136 | return 1; |
| 137 | } else { |
| 138 | ungetc(c, fp); |
| 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 143 | /** |
| 144 | * @brief Open the @p filepath, parse patterns and given string-argument. |
| 145 | * |
| 146 | * @param[in] filepath File to parse. Contains patterns and string. |
| 147 | * @param[out] infile The file descriptor of @p filepath. |
| 148 | * @param[out] patterns Storage of patterns. |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 149 | * @param[out] patterns_count Number of items in @p patterns. |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 150 | * @param[out] strarg The string-argument to check. |
| 151 | * @return 0 on success. |
| 152 | */ |
| 153 | static int |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 154 | parse_patterns_file(const char *filepath, FILE **infile, struct yr_pattern **patterns, int *patterns_count, char **strarg) |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 155 | { |
| 156 | int blankline = 0; |
| 157 | char *str = NULL; |
| 158 | size_t len = 0; |
| 159 | ssize_t l; |
| 160 | |
| 161 | *infile = fopen(filepath, "rb"); |
| 162 | if (!(*infile)) { |
| 163 | fprintf(stderr, "yangre error: unable to open input file %s (%s).\n", optarg, strerror(errno)); |
| 164 | goto error; |
| 165 | } |
aPiecek | ac2c282 | 2023-07-04 10:15:56 +0200 | [diff] [blame] | 166 | if (file_is_empty(*infile)) { |
| 167 | if (create_empty_string(strarg)) { |
| 168 | goto error; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 172 | |
| 173 | while ((l = getline(&str, &len, *infile)) != -1) { |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame] | 174 | if (!blankline && ((str[0] == '\n') || ((str[0] == '\r') && (str[1] == '\n')))) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 175 | /* blank line */ |
| 176 | blankline = 1; |
| 177 | continue; |
| 178 | } |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame] | 179 | if ((str[0] != '\n') && (str[0] != '\r') && (str[l - 1] == '\n')) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 180 | /* remove ending newline */ |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame] | 181 | if ((l > 1) && (str[l - 2] == '\r') && (str[l - 1] == '\n')) { |
| 182 | str[l - 2] = '\0'; |
| 183 | } else { |
| 184 | str[l - 1] = '\0'; |
| 185 | } |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 186 | } |
| 187 | if (blankline) { |
| 188 | /* done - str is now the string to check */ |
| 189 | blankline = 0; |
| 190 | *strarg = str; |
| 191 | break; |
| 192 | /* else read the patterns */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 193 | } else if (add_pattern(patterns, patterns_count, (str[0] == ' ') ? &str[1] : str)) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 194 | goto error; |
| 195 | } |
| 196 | if (str[0] == ' ') { |
| 197 | /* set invert-match */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 198 | (*patterns)[*patterns_count - 1].invert = 1; |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame] | 201 | if (!str || (blankline && (str[0] != '\0'))) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 202 | /* corner case, no input after blankline meaning the pattern to check is empty */ |
aPiecek | ac2c282 | 2023-07-04 10:15:56 +0200 | [diff] [blame] | 203 | if (create_empty_string(&str)) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 204 | goto error; |
| 205 | } |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 206 | } |
| 207 | *strarg = str; |
| 208 | |
| 209 | return 0; |
| 210 | |
| 211 | error: |
| 212 | free(str); |
| 213 | if (*infile) { |
| 214 | fclose(*infile); |
| 215 | *infile = NULL; |
| 216 | } |
| 217 | *strarg = NULL; |
| 218 | |
| 219 | return 1; |
| 220 | } |
| 221 | |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 222 | static char * |
| 223 | modstr_init(void) |
| 224 | { |
| 225 | const char *module_start = "module yangre {" |
| 226 | "yang-version 1.1;" |
| 227 | "namespace urn:cesnet:libyang:yangre;" |
| 228 | "prefix re;" |
| 229 | "leaf pattern {" |
| 230 | " type string {"; |
| 231 | |
| 232 | return strdup(module_start); |
| 233 | } |
| 234 | |
| 235 | static char * |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 236 | modstr_add_pattern(char **modstr, const struct yr_pattern *pattern) |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 237 | { |
| 238 | char *new; |
| 239 | const char *module_invertmatch = " { modifier invert-match; }"; |
| 240 | const char *module_match = ";"; |
| 241 | |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 242 | if (asprintf(&new, "%s pattern %s%s", *modstr, pattern->expr, |
| 243 | pattern->invert ? module_invertmatch : module_match) == -1) { |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 244 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 245 | return NULL; |
| 246 | } |
| 247 | free(*modstr); |
| 248 | *modstr = NULL; |
| 249 | |
| 250 | return new; |
| 251 | } |
| 252 | |
| 253 | static char * |
| 254 | modstr_add_ending(char **modstr) |
| 255 | { |
| 256 | char *new; |
| 257 | static const char *module_end = "}}}"; |
| 258 | |
| 259 | if (asprintf(&new, "%s%s", *modstr, module_end) == -1) { |
| 260 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 261 | return NULL; |
| 262 | } |
| 263 | free(*modstr); |
| 264 | *modstr = NULL; |
| 265 | |
| 266 | return new; |
| 267 | } |
| 268 | |
| 269 | static int |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 270 | create_module(struct yr_pattern *patterns, int patterns_count, char **mod) |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 271 | { |
| 272 | int i; |
| 273 | char *new = NULL, *modstr; |
| 274 | |
| 275 | if (!(modstr = modstr_init())) { |
| 276 | goto error; |
| 277 | } |
| 278 | |
| 279 | for (i = 0; i < patterns_count; i++) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 280 | if (!(new = modstr_add_pattern(&modstr, &patterns[i]))) { |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 281 | goto error; |
| 282 | } |
| 283 | modstr = new; |
| 284 | } |
| 285 | |
| 286 | if (!(new = modstr_add_ending(&modstr))) { |
| 287 | goto error; |
| 288 | } |
| 289 | |
| 290 | *mod = new; |
| 291 | |
| 292 | return 0; |
| 293 | |
| 294 | error: |
| 295 | *mod = NULL; |
| 296 | free(new); |
| 297 | free(modstr); |
| 298 | |
| 299 | return 1; |
| 300 | } |
| 301 | |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 302 | static void |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 303 | print_verbose(struct yr_pattern *patterns, int patterns_count, char *str, LY_ERR match) |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 304 | { |
| 305 | int i; |
| 306 | |
| 307 | for (i = 0; i < patterns_count; i++) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 308 | fprintf(stdout, "pattern %d: %s\n", i + 1, patterns[i].expr); |
| 309 | fprintf(stdout, "matching %d: %s\n", i + 1, patterns[i].invert ? "inverted" : "regular"); |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 310 | } |
| 311 | fprintf(stdout, "string : %s\n", str); |
| 312 | if (match == LY_SUCCESS) { |
| 313 | fprintf(stdout, "result : matching\n"); |
| 314 | } else if (match == LY_EVALID) { |
| 315 | fprintf(stdout, "result : not matching\n"); |
| 316 | } else { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 317 | fprintf(stdout, "result : error (%s)\n", ly_last_logmsg()); |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 321 | int |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 322 | main(int argc, char *argv[]) |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 323 | { |
| 324 | LY_ERR match; |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 325 | int i, opt_index = 0, ret = 1, verbose = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 326 | struct option options[] = { |
| 327 | {"help", no_argument, NULL, 'h'}, |
| 328 | {"file", required_argument, NULL, 'f'}, |
| 329 | {"invert-match", no_argument, NULL, 'i'}, |
| 330 | {"pattern", required_argument, NULL, 'p'}, |
| 331 | {"version", no_argument, NULL, 'v'}, |
| 332 | {"verbose", no_argument, NULL, 'V'}, |
| 333 | {NULL, 0, NULL, 0} |
| 334 | }; |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 335 | struct yr_pattern *patterns = NULL; |
| 336 | char *str = NULL, *modstr = NULL; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 337 | int patterns_count = 0; |
| 338 | struct ly_ctx *ctx = NULL; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 339 | struct lys_module *mod; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 340 | FILE *infile = NULL; |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 341 | ly_bool info_printed = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 342 | |
| 343 | opterr = 0; |
| 344 | while ((i = getopt_long(argc, argv, "hf:ivVp:", options, &opt_index)) != -1) { |
| 345 | switch (i) { |
| 346 | case 'h': |
| 347 | help(); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 348 | info_printed = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 349 | break; |
| 350 | case 'f': |
| 351 | if (infile) { |
| 352 | help(); |
| 353 | fprintf(stderr, "yangre error: multiple input files are not supported.\n"); |
| 354 | goto cleanup; |
| 355 | } else if (patterns_count) { |
| 356 | help(); |
| 357 | fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n"); |
| 358 | goto cleanup; |
| 359 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 360 | if (parse_patterns_file(optarg, &infile, &patterns, &patterns_count, &str)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 361 | goto cleanup; |
| 362 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 363 | break; |
| 364 | case 'i': |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 365 | if (!patterns_count || patterns[patterns_count - 1].invert) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 366 | help(); |
| 367 | fprintf(stderr, "yangre error: invert-match option must follow some pattern.\n"); |
| 368 | goto cleanup; |
| 369 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 370 | patterns[patterns_count - 1].invert = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 371 | break; |
| 372 | case 'p': |
| 373 | if (infile) { |
| 374 | help(); |
| 375 | fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n"); |
| 376 | goto cleanup; |
| 377 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 378 | if (add_pattern(&patterns, &patterns_count, optarg)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 379 | goto cleanup; |
| 380 | } |
| 381 | break; |
| 382 | case 'v': |
| 383 | version(); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 384 | info_printed = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 385 | break; |
| 386 | case 'V': |
| 387 | verbose = 1; |
| 388 | break; |
| 389 | default: |
| 390 | help(); |
| 391 | if (optopt) { |
| 392 | fprintf(stderr, "yangre error: invalid option: -%c\n", optopt); |
| 393 | } else { |
| 394 | fprintf(stderr, "yangre error: invalid option: %s\n", argv[optind - 1]); |
| 395 | } |
| 396 | goto cleanup; |
| 397 | } |
| 398 | } |
| 399 | |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 400 | if (info_printed) { |
| 401 | ret = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 402 | goto cleanup; |
| 403 | } |
| 404 | |
| 405 | if (!str) { |
| 406 | /* check options compatibility */ |
| 407 | if (optind >= argc) { |
| 408 | help(); |
| 409 | fprintf(stderr, "yangre error: missing <string> parameter to process.\n"); |
| 410 | goto cleanup; |
| 411 | } else if (!patterns_count) { |
| 412 | help(); |
| 413 | fprintf(stderr, "yangre error: missing pattern parameter to use.\n"); |
| 414 | goto cleanup; |
| 415 | } |
| 416 | str = argv[optind]; |
| 417 | } |
| 418 | |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 419 | if (create_module(patterns, patterns_count, &modstr)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 420 | goto cleanup; |
| 421 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 422 | |
| 423 | if (ly_ctx_new(NULL, 0, &ctx)) { |
| 424 | goto cleanup; |
| 425 | } |
| 426 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 427 | ly_set_log_clb(pattern_error); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 428 | if (lys_parse_mem(ctx, modstr, LYS_IN_YANG, &mod) || !mod->compiled || !mod->compiled->data) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 429 | goto cleanup; |
| 430 | } |
| 431 | |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 432 | /* check the value */ |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 433 | match = lyd_value_validate(ctx, mod->compiled->data, str, strlen(str), NULL, NULL, NULL); |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 434 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 435 | if (verbose) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 436 | print_verbose(patterns, patterns_count, str, match); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 437 | } |
| 438 | if (match == LY_SUCCESS) { |
| 439 | ret = 0; |
| 440 | } else if (match == LY_EVALID) { |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 441 | ret = 2; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 442 | } else { |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 443 | ret = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | cleanup: |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 447 | ly_ctx_destroy(ctx); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 448 | for (i = 0; i < patterns_count; i++) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 449 | free(patterns[i].expr); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 450 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 451 | if (patterns_count) { |
| 452 | free(patterns); |
| 453 | } |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 454 | free(modstr); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 455 | if (infile) { |
| 456 | fclose(infile); |
| 457 | free(str); |
| 458 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 459 | |
| 460 | return ret; |
| 461 | } |