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 |
| 76 | pattern_error(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 77 | { |
| 78 | (void) path; /* unused */ |
| 79 | |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 80 | if (level == LY_LLERR) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 81 | fprintf(stderr, "yangre error: %s\n", msg); |
| 82 | } |
| 83 | } |
| 84 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 85 | static int |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 86 | add_pattern(struct yr_pattern **patterns, int *counter, char *pattern) |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 87 | { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 88 | void *reallocated; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 89 | int orig_counter; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 90 | |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 91 | /* Store the original number of items. */ |
| 92 | orig_counter = *counter; |
| 93 | |
| 94 | /* Reallocate 'patterns' memory with additional space. */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 95 | reallocated = realloc(*patterns, (orig_counter + 1) * sizeof **patterns); |
| 96 | if (!reallocated) { |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 97 | goto error; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 98 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 99 | (*patterns) = reallocated; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 100 | /* Allocated memory is now larger. */ |
| 101 | (*counter)++; |
| 102 | /* Copy the pattern and store it to the additonal space. */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 103 | (*patterns)[orig_counter].expr = strdup(pattern); |
| 104 | if (!(*patterns)[orig_counter].expr) { |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 105 | goto error; |
| 106 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 107 | (*patterns)[orig_counter].invert = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 108 | |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 109 | return 0; |
aPiecek | b2b5136 | 2023-06-22 09:14:59 +0200 | [diff] [blame] | 110 | |
| 111 | error: |
| 112 | fprintf(stderr, "yangre error: memory allocation error.\n"); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 113 | return 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 114 | } |
| 115 | |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 116 | /** |
| 117 | * @brief Open the @p filepath, parse patterns and given string-argument. |
| 118 | * |
| 119 | * @param[in] filepath File to parse. Contains patterns and string. |
| 120 | * @param[out] infile The file descriptor of @p filepath. |
| 121 | * @param[out] patterns Storage of patterns. |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 122 | * @param[out] patterns_count Number of items in @p patterns. |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 123 | * @param[out] strarg The string-argument to check. |
| 124 | * @return 0 on success. |
| 125 | */ |
| 126 | static int |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 127 | 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] | 128 | { |
| 129 | int blankline = 0; |
| 130 | char *str = NULL; |
| 131 | size_t len = 0; |
| 132 | ssize_t l; |
| 133 | |
| 134 | *infile = fopen(filepath, "rb"); |
| 135 | if (!(*infile)) { |
| 136 | fprintf(stderr, "yangre error: unable to open input file %s (%s).\n", optarg, strerror(errno)); |
| 137 | goto error; |
| 138 | } |
| 139 | |
| 140 | while ((l = getline(&str, &len, *infile)) != -1) { |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 141 | if (!blankline && ((str[0] == '\n') || ((str[0] == '\r') && (str[1] == '\n')))) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 142 | /* blank line */ |
| 143 | blankline = 1; |
| 144 | continue; |
| 145 | } |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 146 | if ((str[0] != '\n') && (str[0] != '\r') && (str[l - 1] == '\n')) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 147 | /* remove ending newline */ |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 148 | if ((l > 1) && (str[l - 2] == '\r') && (str[l - 1] == '\n')) { |
| 149 | str[l - 2] = '\0'; |
| 150 | } else { |
| 151 | str[l - 1] = '\0'; |
| 152 | } |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 153 | } |
| 154 | if (blankline) { |
| 155 | /* done - str is now the string to check */ |
| 156 | blankline = 0; |
| 157 | *strarg = str; |
| 158 | break; |
| 159 | /* else read the patterns */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 160 | } else if (add_pattern(patterns, patterns_count, (str[0] == ' ') ? &str[1] : str)) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 161 | goto error; |
| 162 | } |
| 163 | if (str[0] == ' ') { |
| 164 | /* set invert-match */ |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 165 | (*patterns)[*patterns_count - 1].invert = 1; |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 166 | } |
| 167 | } |
lePici | f71fd7d | 2023-07-04 08:07:01 +0200 | [diff] [blame^] | 168 | if (!str || (blankline && (str[0] != '\0'))) { |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 169 | /* corner case, no input after blankline meaning the pattern to check is empty */ |
| 170 | free(str); |
| 171 | str = malloc(sizeof(char)); |
| 172 | if (!str) { |
| 173 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 174 | goto error; |
| 175 | } |
| 176 | str[0] = '\0'; |
| 177 | } |
| 178 | *strarg = str; |
| 179 | |
| 180 | return 0; |
| 181 | |
| 182 | error: |
| 183 | free(str); |
| 184 | if (*infile) { |
| 185 | fclose(*infile); |
| 186 | *infile = NULL; |
| 187 | } |
| 188 | *strarg = NULL; |
| 189 | |
| 190 | return 1; |
| 191 | } |
| 192 | |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 193 | static char * |
| 194 | modstr_init(void) |
| 195 | { |
| 196 | const char *module_start = "module yangre {" |
| 197 | "yang-version 1.1;" |
| 198 | "namespace urn:cesnet:libyang:yangre;" |
| 199 | "prefix re;" |
| 200 | "leaf pattern {" |
| 201 | " type string {"; |
| 202 | |
| 203 | return strdup(module_start); |
| 204 | } |
| 205 | |
| 206 | static char * |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 207 | modstr_add_pattern(char **modstr, const struct yr_pattern *pattern) |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 208 | { |
| 209 | char *new; |
| 210 | const char *module_invertmatch = " { modifier invert-match; }"; |
| 211 | const char *module_match = ";"; |
| 212 | |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 213 | if (asprintf(&new, "%s pattern %s%s", *modstr, pattern->expr, |
| 214 | pattern->invert ? module_invertmatch : module_match) == -1) { |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 215 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 216 | return NULL; |
| 217 | } |
| 218 | free(*modstr); |
| 219 | *modstr = NULL; |
| 220 | |
| 221 | return new; |
| 222 | } |
| 223 | |
| 224 | static char * |
| 225 | modstr_add_ending(char **modstr) |
| 226 | { |
| 227 | char *new; |
| 228 | static const char *module_end = "}}}"; |
| 229 | |
| 230 | if (asprintf(&new, "%s%s", *modstr, module_end) == -1) { |
| 231 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 232 | return NULL; |
| 233 | } |
| 234 | free(*modstr); |
| 235 | *modstr = NULL; |
| 236 | |
| 237 | return new; |
| 238 | } |
| 239 | |
| 240 | static int |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 241 | create_module(struct yr_pattern *patterns, int patterns_count, char **mod) |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 242 | { |
| 243 | int i; |
| 244 | char *new = NULL, *modstr; |
| 245 | |
| 246 | if (!(modstr = modstr_init())) { |
| 247 | goto error; |
| 248 | } |
| 249 | |
| 250 | for (i = 0; i < patterns_count; i++) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 251 | if (!(new = modstr_add_pattern(&modstr, &patterns[i]))) { |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 252 | goto error; |
| 253 | } |
| 254 | modstr = new; |
| 255 | } |
| 256 | |
| 257 | if (!(new = modstr_add_ending(&modstr))) { |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | *mod = new; |
| 262 | |
| 263 | return 0; |
| 264 | |
| 265 | error: |
| 266 | *mod = NULL; |
| 267 | free(new); |
| 268 | free(modstr); |
| 269 | |
| 270 | return 1; |
| 271 | } |
| 272 | |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 273 | static void |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 274 | print_verbose(struct ly_ctx *ctx, struct yr_pattern *patterns, int patterns_count, char *str, LY_ERR match) |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 275 | { |
| 276 | int i; |
| 277 | |
| 278 | for (i = 0; i < patterns_count; i++) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 279 | fprintf(stdout, "pattern %d: %s\n", i + 1, patterns[i].expr); |
| 280 | fprintf(stdout, "matching %d: %s\n", i + 1, patterns[i].invert ? "inverted" : "regular"); |
aPiecek | 44ffabc | 2023-06-30 11:32:11 +0200 | [diff] [blame] | 281 | } |
| 282 | fprintf(stdout, "string : %s\n", str); |
| 283 | if (match == LY_SUCCESS) { |
| 284 | fprintf(stdout, "result : matching\n"); |
| 285 | } else if (match == LY_EVALID) { |
| 286 | fprintf(stdout, "result : not matching\n"); |
| 287 | } else { |
| 288 | fprintf(stdout, "result : error (%s)\n", ly_errmsg(ctx)); |
| 289 | } |
| 290 | } |
| 291 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 292 | int |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 293 | main(int argc, char *argv[]) |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 294 | { |
| 295 | LY_ERR match; |
aPiecek | e29b164 | 2023-06-30 10:28:14 +0200 | [diff] [blame] | 296 | int i, opt_index = 0, ret = 1, verbose = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 297 | struct option options[] = { |
| 298 | {"help", no_argument, NULL, 'h'}, |
| 299 | {"file", required_argument, NULL, 'f'}, |
| 300 | {"invert-match", no_argument, NULL, 'i'}, |
| 301 | {"pattern", required_argument, NULL, 'p'}, |
| 302 | {"version", no_argument, NULL, 'v'}, |
| 303 | {"verbose", no_argument, NULL, 'V'}, |
| 304 | {NULL, 0, NULL, 0} |
| 305 | }; |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 306 | struct yr_pattern *patterns = NULL; |
| 307 | char *str = NULL, *modstr = NULL; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 308 | int patterns_count = 0; |
| 309 | struct ly_ctx *ctx = NULL; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 310 | struct lys_module *mod; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 311 | FILE *infile = NULL; |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 312 | ly_bool info_printed = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 313 | |
| 314 | opterr = 0; |
| 315 | while ((i = getopt_long(argc, argv, "hf:ivVp:", options, &opt_index)) != -1) { |
| 316 | switch (i) { |
| 317 | case 'h': |
| 318 | help(); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 319 | info_printed = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 320 | break; |
| 321 | case 'f': |
| 322 | if (infile) { |
| 323 | help(); |
| 324 | fprintf(stderr, "yangre error: multiple input files are not supported.\n"); |
| 325 | goto cleanup; |
| 326 | } else if (patterns_count) { |
| 327 | help(); |
| 328 | fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n"); |
| 329 | goto cleanup; |
| 330 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 331 | if (parse_patterns_file(optarg, &infile, &patterns, &patterns_count, &str)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 332 | goto cleanup; |
| 333 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 334 | break; |
| 335 | case 'i': |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 336 | if (!patterns_count || patterns[patterns_count - 1].invert) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 337 | help(); |
| 338 | fprintf(stderr, "yangre error: invert-match option must follow some pattern.\n"); |
| 339 | goto cleanup; |
| 340 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 341 | patterns[patterns_count - 1].invert = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 342 | break; |
| 343 | case 'p': |
| 344 | if (infile) { |
| 345 | help(); |
| 346 | fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n"); |
| 347 | goto cleanup; |
| 348 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 349 | if (add_pattern(&patterns, &patterns_count, optarg)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 350 | goto cleanup; |
| 351 | } |
| 352 | break; |
| 353 | case 'v': |
| 354 | version(); |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 355 | info_printed = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 356 | break; |
| 357 | case 'V': |
| 358 | verbose = 1; |
| 359 | break; |
| 360 | default: |
| 361 | help(); |
| 362 | if (optopt) { |
| 363 | fprintf(stderr, "yangre error: invalid option: -%c\n", optopt); |
| 364 | } else { |
| 365 | fprintf(stderr, "yangre error: invalid option: %s\n", argv[optind - 1]); |
| 366 | } |
| 367 | goto cleanup; |
| 368 | } |
| 369 | } |
| 370 | |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 371 | if (info_printed) { |
| 372 | ret = 0; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 373 | goto cleanup; |
| 374 | } |
| 375 | |
| 376 | if (!str) { |
| 377 | /* check options compatibility */ |
| 378 | if (optind >= argc) { |
| 379 | help(); |
| 380 | fprintf(stderr, "yangre error: missing <string> parameter to process.\n"); |
| 381 | goto cleanup; |
| 382 | } else if (!patterns_count) { |
| 383 | help(); |
| 384 | fprintf(stderr, "yangre error: missing pattern parameter to use.\n"); |
| 385 | goto cleanup; |
| 386 | } |
| 387 | str = argv[optind]; |
| 388 | } |
| 389 | |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 390 | if (create_module(patterns, patterns_count, &modstr)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 391 | goto cleanup; |
| 392 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 393 | |
| 394 | if (ly_ctx_new(NULL, 0, &ctx)) { |
| 395 | goto cleanup; |
| 396 | } |
| 397 | |
| 398 | ly_set_log_clb(pattern_error, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 399 | 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] | 400 | goto cleanup; |
| 401 | } |
| 402 | |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 403 | /* check the value */ |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 404 | 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] | 405 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 406 | if (verbose) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 407 | print_verbose(ctx, patterns, patterns_count, str, match); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 408 | } |
| 409 | if (match == LY_SUCCESS) { |
| 410 | ret = 0; |
| 411 | } else if (match == LY_EVALID) { |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 412 | ret = 2; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 413 | } else { |
aPiecek | bfdf83b | 2023-06-29 13:55:09 +0200 | [diff] [blame] | 414 | ret = 1; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | cleanup: |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 418 | ly_ctx_destroy(ctx); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 419 | for (i = 0; i < patterns_count; i++) { |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 420 | free(patterns[i].expr); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 421 | } |
aPiecek | 834d73b | 2023-06-30 14:33:02 +0200 | [diff] [blame] | 422 | if (patterns_count) { |
| 423 | free(patterns); |
| 424 | } |
aPiecek | c5cf371 | 2023-06-30 11:27:33 +0200 | [diff] [blame] | 425 | free(modstr); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 426 | if (infile) { |
| 427 | fclose(infile); |
| 428 | free(str); |
| 429 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 430 | |
| 431 | return ret; |
| 432 | } |