blob: c71bd0efda2eea0e2ded621507a98401da1e578b [file] [log] [blame]
Radek Krejci5819f7c2019-05-31 14:53:29 +02001/**
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 Hopps32874e12021-05-01 09:43:54 -040015#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejci5819f7c2019-05-31 14:53:29 +020016
aPieceke29b1642023-06-30 10:28:14 +020017#include <assert.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020018#include <errno.h>
Radek Krejci4001a102020-11-13 15:43:05 +010019#include <getopt.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020020#include <stdio.h>
21#include <stdlib.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020022#include <string.h>
Radek Krejci4001a102020-11-13 15:43:05 +010023#include <sys/types.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "libyang.h"
26
Michal Vasko5aa44c02020-06-29 11:47:02 +020027#include "compat.h"
Radek Krejci4001a102020-11-13 15:43:05 +010028#include "tools/config.h"
Radek Krejci5819f7c2019-05-31 14:53:29 +020029
30void
31help(void)
32{
33 fprintf(stdout, "YANG Regular Expressions processor.\n");
34 fprintf(stdout, "Usage:\n");
35 fprintf(stdout, " yangre [-hv]\n");
36 fprintf(stdout, " yangre [-V] -p <regexp1> [-i] [-p <regexp2> [-i] ...] <string>\n");
37 fprintf(stdout, " yangre [-V] -f <file>\n");
aPiecekbfdf83b2023-06-29 13:55:09 +020038 fprintf(stdout, "Returns 0 if string matches the pattern(s) or if otherwise successful.\n");
39 fprintf(stdout, "Returns 1 on error.\n");
40 fprintf(stdout, "Returns 2 if string does not match the pattern(s).\n\n");
Radek Krejci5819f7c2019-05-31 14:53:29 +020041 fprintf(stdout, "Options:\n"
Radek Krejci4001a102020-11-13 15:43:05 +010042 " -h, --help Show this help message and exit.\n"
43 " -v, --version Show version number and exit.\n"
44 " -V, --verbose Print the processing information.\n"
45 " -i, --invert-match Invert-match modifier for the closest preceding\n"
46 " pattern.\n"
47 " -p, --pattern=\"REGEXP\" Regular expression including the quoting,\n"
48 " which is applied the same way as in a YANG module.\n"
49 " -f, --file=\"FILE\" List of patterns and the <string> (separated by an\n"
50 " empty line) are taken from <file>. Invert-match is\n"
51 " indicated by the single space character at the \n"
52 " beginning of the pattern line. YANG quotation around\n"
53 " patterns is still expected, but that avoids issues with\n"
54 " reading quotation by shell. Avoid newline at the end\n"
55 " of the string line to represent empty <string>.");
Radek Krejci5819f7c2019-05-31 14:53:29 +020056 fprintf(stdout, "Examples:\n"
Radek Krejci4001a102020-11-13 15:43:05 +010057 " pattern \"[0-9a-fA-F]*\"; -> yangre -p '\"[0-9a-fA-F]*\"' '1F'\n"
58 " pattern '[a-zA-Z0-9\\-_.]*'; -> yangre -p \"'[a-zA-Z0-9\\-_.]*'\" 'a-b'\n"
59 " pattern [xX][mM][lL].*; -> yangre -p '[xX][mM][lL].*' 'xml-encoding'\n\n");
Radek Krejci5819f7c2019-05-31 14:53:29 +020060 fprintf(stdout, "Note that to pass YANG quoting through your shell, you are supposed to use\n"
Radek Krejci4001a102020-11-13 15:43:05 +010061 "the other quotation around. For not-quoted patterns, use single quotes.\n\n");
Radek Krejci5819f7c2019-05-31 14:53:29 +020062}
63
64void
65version(void)
66{
67 fprintf(stdout, "yangre %s\n", PROJECT_VERSION);
68}
69
70void
71pattern_error(LY_LOG_LEVEL level, const char *msg, const char *path)
72{
73 (void) path; /* unused */
74
Radek Krejcid7857262020-11-06 10:29:22 +010075 if (level == LY_LLERR) {
Radek Krejci5819f7c2019-05-31 14:53:29 +020076 fprintf(stderr, "yangre error: %s\n", msg);
77 }
78}
79
80static const char *module_start = "module yangre {"
Radek Krejci4001a102020-11-13 15:43:05 +010081 "yang-version 1.1;"
82 "namespace urn:cesnet:libyang:yangre;"
83 "prefix re;"
84 "leaf pattern {"
85 " type string {";
Radek Krejci5819f7c2019-05-31 14:53:29 +020086static const char *module_invertmatch = " { modifier invert-match; }";
87static const char *module_match = ";";
88static const char *module_end = "}}}";
89
90static int
91add_pattern(char ***patterns, int **inverts, int *counter, char *pattern)
92{
93 void *reallocated1, *reallocated2;
aPiecekb2b51362023-06-22 09:14:59 +020094 int orig_counter;
Radek Krejci5819f7c2019-05-31 14:53:29 +020095
aPiecekb2b51362023-06-22 09:14:59 +020096 /* Store the original number of items. */
97 orig_counter = *counter;
98
99 /* Reallocate 'patterns' memory with additional space. */
100 reallocated1 = realloc(*patterns, (orig_counter + 1) * sizeof **patterns);
101 if (!reallocated1) {
102 goto error;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200103 }
104 (*patterns) = reallocated1;
aPiecekb2b51362023-06-22 09:14:59 +0200105 /* Allocated memory is now larger. */
106 (*counter)++;
107 /* Copy the pattern and store it to the additonal space. */
108 (*patterns)[orig_counter] = strdup(pattern);
109 if (!(*patterns)[orig_counter]) {
110 goto error;
111 }
112
113 /* Reallocate 'inverts' memory with additional space. */
114 reallocated2 = realloc(*inverts, (orig_counter + 1) * sizeof **inverts);
115 if (!reallocated2) {
116 goto error;
117 }
Radek Krejci5819f7c2019-05-31 14:53:29 +0200118 (*inverts) = reallocated2;
aPiecekb2b51362023-06-22 09:14:59 +0200119 (*inverts)[orig_counter] = 0;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200120
aPiecekbfdf83b2023-06-29 13:55:09 +0200121 return 0;
aPiecekb2b51362023-06-22 09:14:59 +0200122
123error:
124 fprintf(stderr, "yangre error: memory allocation error.\n");
aPiecekbfdf83b2023-06-29 13:55:09 +0200125 return 1;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200126}
127
aPieceke29b1642023-06-30 10:28:14 +0200128/**
129 * @brief Open the @p filepath, parse patterns and given string-argument.
130 *
131 * @param[in] filepath File to parse. Contains patterns and string.
132 * @param[out] infile The file descriptor of @p filepath.
133 * @param[out] patterns Storage of patterns.
134 * @param[out] invert_match Array of flags indicates which patterns are inverted.
135 * @param[out] patterns_count Number of items in @p patterns (and @p invert_match too).
136 * @param[out] strarg The string-argument to check.
137 * @return 0 on success.
138 */
139static int
140parse_patterns_file(const char *filepath, FILE **infile, char ***patterns, int **invert_match, int *patterns_count,
141 char **strarg)
142{
143 int blankline = 0;
144 char *str = NULL;
145 size_t len = 0;
146 ssize_t l;
147
148 *infile = fopen(filepath, "rb");
149 if (!(*infile)) {
150 fprintf(stderr, "yangre error: unable to open input file %s (%s).\n", optarg, strerror(errno));
151 goto error;
152 }
153
154 while ((l = getline(&str, &len, *infile)) != -1) {
155 if (!blankline && (str[0] == '\n')) {
156 /* blank line */
157 blankline = 1;
158 continue;
159 }
160 if ((str[0] != '\n') && (str[l - 1] == '\n')) {
161 /* remove ending newline */
162 str[l - 1] = '\0';
163 }
164 if (blankline) {
165 /* done - str is now the string to check */
166 blankline = 0;
167 *strarg = str;
168 break;
169 /* else read the patterns */
170 } else if (add_pattern(patterns, invert_match, patterns_count,
171 (str[0] == ' ') ? &str[1] : str)) {
172 goto error;
173 }
174 if (str[0] == ' ') {
175 /* set invert-match */
176 (*invert_match)[*patterns_count - 1] = 1;
177 }
178 }
179 assert(str);
180 if (blankline && (str[0] != '\0')) {
181 /* corner case, no input after blankline meaning the pattern to check is empty */
182 free(str);
183 str = malloc(sizeof(char));
184 if (!str) {
185 fprintf(stderr, "yangre error: memory allocation failed.\n");
186 goto error;
187 }
188 str[0] = '\0';
189 }
190 *strarg = str;
191
192 return 0;
193
194error:
195 free(str);
196 if (*infile) {
197 fclose(*infile);
198 *infile = NULL;
199 }
200 *strarg = NULL;
201
202 return 1;
203}
204
Radek Krejci5819f7c2019-05-31 14:53:29 +0200205int
Radek Krejci4001a102020-11-13 15:43:05 +0100206main(int argc, char *argv[])
Radek Krejci5819f7c2019-05-31 14:53:29 +0200207{
208 LY_ERR match;
aPieceke29b1642023-06-30 10:28:14 +0200209 int i, opt_index = 0, ret = 1, verbose = 0;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200210 struct option options[] = {
211 {"help", no_argument, NULL, 'h'},
212 {"file", required_argument, NULL, 'f'},
213 {"invert-match", no_argument, NULL, 'i'},
214 {"pattern", required_argument, NULL, 'p'},
215 {"version", no_argument, NULL, 'v'},
216 {"verbose", no_argument, NULL, 'V'},
217 {NULL, 0, NULL, 0}
218 };
219 char **patterns = NULL, *str = NULL, *modstr = NULL, *s;
220 int *invert_match = NULL;
221 int patterns_count = 0;
222 struct ly_ctx *ctx = NULL;
Michal Vasko4de7d072021-07-09 09:13:18 +0200223 struct lys_module *mod;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200224 FILE *infile = NULL;
aPiecekbfdf83b2023-06-29 13:55:09 +0200225 ly_bool info_printed = 0;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200226
227 opterr = 0;
228 while ((i = getopt_long(argc, argv, "hf:ivVp:", options, &opt_index)) != -1) {
229 switch (i) {
230 case 'h':
231 help();
aPiecekbfdf83b2023-06-29 13:55:09 +0200232 info_printed = 1;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200233 break;
234 case 'f':
235 if (infile) {
236 help();
237 fprintf(stderr, "yangre error: multiple input files are not supported.\n");
238 goto cleanup;
239 } else if (patterns_count) {
240 help();
241 fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n");
242 goto cleanup;
243 }
aPieceke29b1642023-06-30 10:28:14 +0200244 if (parse_patterns_file(optarg, &infile, &patterns, &invert_match, &patterns_count, &str)) {
Radek Krejci5819f7c2019-05-31 14:53:29 +0200245 goto cleanup;
246 }
Radek Krejci5819f7c2019-05-31 14:53:29 +0200247 break;
248 case 'i':
249 if (!patterns_count || invert_match[patterns_count - 1]) {
250 help();
251 fprintf(stderr, "yangre error: invert-match option must follow some pattern.\n");
252 goto cleanup;
253 }
254 invert_match[patterns_count - 1] = 1;
255 break;
256 case 'p':
257 if (infile) {
258 help();
259 fprintf(stderr, "yangre error: command line patterns cannot be mixed with file input.\n");
260 goto cleanup;
261 }
262 if (add_pattern(&patterns, &invert_match, &patterns_count, optarg)) {
263 goto cleanup;
264 }
265 break;
266 case 'v':
267 version();
aPiecekbfdf83b2023-06-29 13:55:09 +0200268 info_printed = 1;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200269 break;
270 case 'V':
271 verbose = 1;
272 break;
273 default:
274 help();
275 if (optopt) {
276 fprintf(stderr, "yangre error: invalid option: -%c\n", optopt);
277 } else {
278 fprintf(stderr, "yangre error: invalid option: %s\n", argv[optind - 1]);
279 }
280 goto cleanup;
281 }
282 }
283
aPiecekbfdf83b2023-06-29 13:55:09 +0200284 if (info_printed) {
285 ret = 0;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200286 goto cleanup;
287 }
288
289 if (!str) {
290 /* check options compatibility */
291 if (optind >= argc) {
292 help();
293 fprintf(stderr, "yangre error: missing <string> parameter to process.\n");
294 goto cleanup;
295 } else if (!patterns_count) {
296 help();
297 fprintf(stderr, "yangre error: missing pattern parameter to use.\n");
298 goto cleanup;
299 }
300 str = argv[optind];
301 }
302
Radek Krejci4001a102020-11-13 15:43:05 +0100303 for (modstr = (char *)module_start, i = 0; i < patterns_count; i++) {
Radek Krejci5819f7c2019-05-31 14:53:29 +0200304 if (asprintf(&s, "%s pattern %s%s", modstr, patterns[i], invert_match[i] ? module_invertmatch : module_match) == -1) {
305 fprintf(stderr, "yangre error: memory allocation failed.\n");
306 goto cleanup;
307 }
308 if (modstr != module_start) {
309 free(modstr);
310 }
311 modstr = s;
312 }
313 if (asprintf(&s, "%s%s", modstr, module_end) == -1) {
314 fprintf(stderr, "yangre error: memory allocation failed.\n");
315 goto cleanup;
316 }
317 if (modstr != module_start) {
318 free(modstr);
319 }
320 modstr = s;
321
322 if (ly_ctx_new(NULL, 0, &ctx)) {
323 goto cleanup;
324 }
325
326 ly_set_log_clb(pattern_error, 0);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200327 if (lys_parse_mem(ctx, modstr, LYS_IN_YANG, &mod) || !mod->compiled || !mod->compiled->data) {
Radek Krejci5819f7c2019-05-31 14:53:29 +0200328 goto cleanup;
329 }
330
Radek Krejcid7857262020-11-06 10:29:22 +0100331 /* check the value */
Michal Vaskoaebbce02021-04-06 09:23:37 +0200332 match = lyd_value_validate(ctx, mod->compiled->data, str, strlen(str), NULL, NULL, NULL);
Radek Krejcid7857262020-11-06 10:29:22 +0100333
Radek Krejci5819f7c2019-05-31 14:53:29 +0200334 if (verbose) {
335 for (i = 0; i < patterns_count; i++) {
336 fprintf(stdout, "pattern %d: %s\n", i + 1, patterns[i]);
337 fprintf(stdout, "matching %d: %s\n", i + 1, invert_match[i] ? "inverted" : "regular");
338 }
339 fprintf(stdout, "string : %s\n", str);
340 if (match == LY_SUCCESS) {
341 fprintf(stdout, "result : matching\n");
342 } else if (match == LY_EVALID) {
343 fprintf(stdout, "result : not matching\n");
344 } else {
Radek Krejcid7857262020-11-06 10:29:22 +0100345 fprintf(stdout, "result : error (%s)\n", ly_errmsg(ctx));
Radek Krejci5819f7c2019-05-31 14:53:29 +0200346 }
347 }
348 if (match == LY_SUCCESS) {
349 ret = 0;
350 } else if (match == LY_EVALID) {
aPiecekbfdf83b2023-06-29 13:55:09 +0200351 ret = 2;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200352 } else {
aPiecekbfdf83b2023-06-29 13:55:09 +0200353 ret = 1;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200354 }
355
356cleanup:
Radek Krejci90ed21e2021-04-12 14:47:46 +0200357 ly_ctx_destroy(ctx);
Radek Krejci5819f7c2019-05-31 14:53:29 +0200358 for (i = 0; i < patterns_count; i++) {
359 free(patterns[i]);
360 }
361 free(patterns);
362 free(invert_match);
aPiecekb0356f42023-06-22 10:13:38 +0200363 if (modstr != module_start) {
364 free(modstr);
365 }
Radek Krejci5819f7c2019-05-31 14:53:29 +0200366 if (infile) {
367 fclose(infile);
368 free(str);
369 }
Radek Krejci5819f7c2019-05-31 14:53:29 +0200370
371 return ret;
372}