Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang's YANG Regular Expression tool |
| 5 | * |
| 6 | * Copyright (c) 2017 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 | |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 16 | |
| 17 | #include <errno.h> |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 18 | #include <getopt.h> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 21 | #include <string.h> |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "libyang.h" |
| 25 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 27 | #include "tools/config.h" |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 28 | |
| 29 | void |
| 30 | help(void) |
| 31 | { |
| 32 | fprintf(stdout, "YANG Regular Expressions processor.\n"); |
| 33 | fprintf(stdout, "Usage:\n"); |
| 34 | fprintf(stdout, " yangre [-hv]\n"); |
| 35 | fprintf(stdout, " yangre [-V] -p <regexp1> [-i] [-p <regexp2> [-i] ...] <string>\n"); |
| 36 | fprintf(stdout, " yangre [-V] -f <file>\n"); |
| 37 | fprintf(stdout, "Returns 0 if string matches the pattern(s), 1 if not and -1 on error.\n\n"); |
| 38 | fprintf(stdout, "Options:\n" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 39 | " -h, --help Show this help message and exit.\n" |
| 40 | " -v, --version Show version number and exit.\n" |
| 41 | " -V, --verbose Print the processing information.\n" |
| 42 | " -i, --invert-match Invert-match modifier for the closest preceding\n" |
| 43 | " pattern.\n" |
| 44 | " -p, --pattern=\"REGEXP\" Regular expression including the quoting,\n" |
| 45 | " which is applied the same way as in a YANG module.\n" |
| 46 | " -f, --file=\"FILE\" List of patterns and the <string> (separated by an\n" |
| 47 | " empty line) are taken from <file>. Invert-match is\n" |
| 48 | " indicated by the single space character at the \n" |
| 49 | " beginning of the pattern line. YANG quotation around\n" |
| 50 | " patterns is still expected, but that avoids issues with\n" |
| 51 | " reading quotation by shell. Avoid newline at the end\n" |
| 52 | " of the string line to represent empty <string>."); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 53 | fprintf(stdout, "Examples:\n" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 54 | " pattern \"[0-9a-fA-F]*\"; -> yangre -p '\"[0-9a-fA-F]*\"' '1F'\n" |
| 55 | " pattern '[a-zA-Z0-9\\-_.]*'; -> yangre -p \"'[a-zA-Z0-9\\-_.]*'\" 'a-b'\n" |
| 56 | " 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] | 57 | 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] | 58 | "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] | 59 | } |
| 60 | |
| 61 | void |
| 62 | version(void) |
| 63 | { |
| 64 | fprintf(stdout, "yangre %s\n", PROJECT_VERSION); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | pattern_error(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 69 | { |
| 70 | (void) path; /* unused */ |
| 71 | |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 72 | if (level == LY_LLERR) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 73 | fprintf(stderr, "yangre error: %s\n", msg); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static const char *module_start = "module yangre {" |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 78 | "yang-version 1.1;" |
| 79 | "namespace urn:cesnet:libyang:yangre;" |
| 80 | "prefix re;" |
| 81 | "leaf pattern {" |
| 82 | " type string {"; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 83 | static const char *module_invertmatch = " { modifier invert-match; }"; |
| 84 | static const char *module_match = ";"; |
| 85 | static const char *module_end = "}}}"; |
| 86 | |
| 87 | static int |
| 88 | add_pattern(char ***patterns, int **inverts, int *counter, char *pattern) |
| 89 | { |
| 90 | void *reallocated1, *reallocated2; |
| 91 | |
| 92 | (*counter)++; |
| 93 | reallocated1 = realloc(*patterns, *counter * sizeof **patterns); |
| 94 | reallocated2 = realloc(*inverts, *counter * sizeof **inverts); |
| 95 | if (!reallocated1 || !reallocated2) { |
| 96 | fprintf(stderr, "yangre error: memory allocation error.\n"); |
| 97 | free(reallocated1); |
| 98 | free(reallocated2); |
| 99 | return EXIT_FAILURE; |
| 100 | } |
| 101 | (*patterns) = reallocated1; |
| 102 | (*patterns)[*counter - 1] = strdup(pattern); |
| 103 | (*inverts) = reallocated2; |
| 104 | (*inverts)[*counter - 1] = 0; |
| 105 | |
| 106 | return EXIT_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | int |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 110 | main(int argc, char *argv[]) |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 111 | { |
| 112 | LY_ERR match; |
| 113 | int i, opt_index = 0, ret = -1, verbose = 0, blankline = 0; |
| 114 | struct option options[] = { |
| 115 | {"help", no_argument, NULL, 'h'}, |
| 116 | {"file", required_argument, NULL, 'f'}, |
| 117 | {"invert-match", no_argument, NULL, 'i'}, |
| 118 | {"pattern", required_argument, NULL, 'p'}, |
| 119 | {"version", no_argument, NULL, 'v'}, |
| 120 | {"verbose", no_argument, NULL, 'V'}, |
| 121 | {NULL, 0, NULL, 0} |
| 122 | }; |
| 123 | char **patterns = NULL, *str = NULL, *modstr = NULL, *s; |
| 124 | int *invert_match = NULL; |
| 125 | int patterns_count = 0; |
| 126 | struct ly_ctx *ctx = NULL; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame^] | 127 | struct lys_module *mod; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 128 | FILE *infile = NULL; |
| 129 | size_t len = 0; |
| 130 | ssize_t l; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 131 | |
| 132 | opterr = 0; |
| 133 | while ((i = getopt_long(argc, argv, "hf:ivVp:", options, &opt_index)) != -1) { |
| 134 | switch (i) { |
| 135 | case 'h': |
| 136 | help(); |
| 137 | ret = -2; /* continue to allow printing version and help at once */ |
| 138 | break; |
| 139 | case 'f': |
| 140 | if (infile) { |
| 141 | help(); |
| 142 | fprintf(stderr, "yangre error: multiple input files are not supported.\n"); |
| 143 | goto cleanup; |
| 144 | } else if (patterns_count) { |
| 145 | help(); |
| 146 | fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n"); |
| 147 | goto cleanup; |
| 148 | } |
| 149 | infile = fopen(optarg, "r"); |
| 150 | if (!infile) { |
| 151 | fprintf(stderr, "yangre error: unable to open input file %s (%s).\n", optarg, strerror(errno)); |
| 152 | goto cleanup; |
| 153 | } |
| 154 | |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 155 | while ((l = getline(&str, &len, infile)) != -1) { |
| 156 | if (!blankline && (str[0] == '\n')) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 157 | /* blank line */ |
| 158 | blankline = 1; |
| 159 | continue; |
| 160 | } |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 161 | if ((str[0] != '\n') && (str[l - 1] == '\n')) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 162 | /* remove ending newline */ |
| 163 | str[l - 1] = '\0'; |
| 164 | } |
| 165 | if (blankline) { |
| 166 | /* done - str is now the string to check */ |
| 167 | blankline = 0; |
| 168 | break; |
| 169 | /* else read the patterns */ |
| 170 | } else if (add_pattern(&patterns, &invert_match, &patterns_count, |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 171 | (str[0] == ' ') ? &str[1] : str)) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 172 | goto cleanup; |
| 173 | } |
| 174 | if (str[0] == ' ') { |
| 175 | /* set invert-match */ |
| 176 | invert_match[patterns_count - 1] = 1; |
| 177 | } |
| 178 | } |
| 179 | if (blankline) { |
| 180 | /* corner case, no input after blankline meaning the pattern to check is empty */ |
| 181 | if (str != NULL) { |
| 182 | free(str); |
| 183 | } |
| 184 | str = malloc(sizeof(char)); |
| 185 | str[0] = '\0'; |
| 186 | } |
| 187 | break; |
| 188 | case 'i': |
| 189 | if (!patterns_count || invert_match[patterns_count - 1]) { |
| 190 | help(); |
| 191 | fprintf(stderr, "yangre error: invert-match option must follow some pattern.\n"); |
| 192 | goto cleanup; |
| 193 | } |
| 194 | invert_match[patterns_count - 1] = 1; |
| 195 | break; |
| 196 | case 'p': |
| 197 | if (infile) { |
| 198 | help(); |
| 199 | fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n"); |
| 200 | goto cleanup; |
| 201 | } |
| 202 | if (add_pattern(&patterns, &invert_match, &patterns_count, optarg)) { |
| 203 | goto cleanup; |
| 204 | } |
| 205 | break; |
| 206 | case 'v': |
| 207 | version(); |
| 208 | ret = -2; /* continue to allow printing version and help at once */ |
| 209 | break; |
| 210 | case 'V': |
| 211 | verbose = 1; |
| 212 | break; |
| 213 | default: |
| 214 | help(); |
| 215 | if (optopt) { |
| 216 | fprintf(stderr, "yangre error: invalid option: -%c\n", optopt); |
| 217 | } else { |
| 218 | fprintf(stderr, "yangre error: invalid option: %s\n", argv[optind - 1]); |
| 219 | } |
| 220 | goto cleanup; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (ret == -2) { |
| 225 | goto cleanup; |
| 226 | } |
| 227 | |
| 228 | if (!str) { |
| 229 | /* check options compatibility */ |
| 230 | if (optind >= argc) { |
| 231 | help(); |
| 232 | fprintf(stderr, "yangre error: missing <string> parameter to process.\n"); |
| 233 | goto cleanup; |
| 234 | } else if (!patterns_count) { |
| 235 | help(); |
| 236 | fprintf(stderr, "yangre error: missing pattern parameter to use.\n"); |
| 237 | goto cleanup; |
| 238 | } |
| 239 | str = argv[optind]; |
| 240 | } |
| 241 | |
Radek Krejci | 4001a10 | 2020-11-13 15:43:05 +0100 | [diff] [blame] | 242 | for (modstr = (char *)module_start, i = 0; i < patterns_count; i++) { |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 243 | if (asprintf(&s, "%s pattern %s%s", modstr, patterns[i], invert_match[i] ? module_invertmatch : module_match) == -1) { |
| 244 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 245 | goto cleanup; |
| 246 | } |
| 247 | if (modstr != module_start) { |
| 248 | free(modstr); |
| 249 | } |
| 250 | modstr = s; |
| 251 | } |
| 252 | if (asprintf(&s, "%s%s", modstr, module_end) == -1) { |
| 253 | fprintf(stderr, "yangre error: memory allocation failed.\n"); |
| 254 | goto cleanup; |
| 255 | } |
| 256 | if (modstr != module_start) { |
| 257 | free(modstr); |
| 258 | } |
| 259 | modstr = s; |
| 260 | |
| 261 | if (ly_ctx_new(NULL, 0, &ctx)) { |
| 262 | goto cleanup; |
| 263 | } |
| 264 | |
| 265 | ly_set_log_clb(pattern_error, 0); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 266 | 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] | 267 | goto cleanup; |
| 268 | } |
| 269 | |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 270 | /* check the value */ |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 271 | 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] | 272 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 273 | if (verbose) { |
| 274 | for (i = 0; i < patterns_count; i++) { |
| 275 | fprintf(stdout, "pattern %d: %s\n", i + 1, patterns[i]); |
| 276 | fprintf(stdout, "matching %d: %s\n", i + 1, invert_match[i] ? "inverted" : "regular"); |
| 277 | } |
| 278 | fprintf(stdout, "string : %s\n", str); |
| 279 | if (match == LY_SUCCESS) { |
| 280 | fprintf(stdout, "result : matching\n"); |
| 281 | } else if (match == LY_EVALID) { |
| 282 | fprintf(stdout, "result : not matching\n"); |
| 283 | } else { |
Radek Krejci | d785726 | 2020-11-06 10:29:22 +0100 | [diff] [blame] | 284 | fprintf(stdout, "result : error (%s)\n", ly_errmsg(ctx)); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | if (match == LY_SUCCESS) { |
| 288 | ret = 0; |
| 289 | } else if (match == LY_EVALID) { |
| 290 | ret = 1; |
| 291 | } else { |
| 292 | ret = -1; |
| 293 | } |
| 294 | |
| 295 | cleanup: |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 296 | ly_ctx_destroy(ctx); |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 297 | for (i = 0; i < patterns_count; i++) { |
| 298 | free(patterns[i]); |
| 299 | } |
| 300 | free(patterns); |
| 301 | free(invert_match); |
| 302 | free(modstr); |
| 303 | if (infile) { |
| 304 | fclose(infile); |
| 305 | free(str); |
| 306 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 307 | |
| 308 | return ret; |
| 309 | } |