blob: 1cec6debdcfaa4e0ce15c58827d0e664c89507c2 [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejci5819f7c2019-05-31 14:53:29 +020016
17#include <errno.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020018#include <stdio.h>
19#include <stdlib.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020020#include <sys/types.h>
21#include <string.h>
22#include <getopt.h>
Radek Krejci5819f7c2019-05-31 14:53:29 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "libyang.h"
25
26#include "tools/config.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020027#include "compat.h"
Radek Krejci5819f7c2019-05-31 14:53:29 +020028
29void
30help(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"
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>.");
53 fprintf(stdout, "Examples:\n"
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");
57 fprintf(stdout, "Note that to pass YANG quoting through your shell, you are supposed to use\n"
58 "the other quotation around. For not-quoted patterns, use single quotes.\n\n");
59}
60
61void
62version(void)
63{
64 fprintf(stdout, "yangre %s\n", PROJECT_VERSION);
65}
66
67void
68pattern_error(LY_LOG_LEVEL level, const char *msg, const char *path)
69{
70 (void) path; /* unused */
71
Radek Krejcid7857262020-11-06 10:29:22 +010072 if (level == LY_LLERR) {
Radek Krejci5819f7c2019-05-31 14:53:29 +020073 fprintf(stderr, "yangre error: %s\n", msg);
74 }
75}
76
77static const char *module_start = "module yangre {"
78 "yang-version 1.1;"
79 "namespace urn:cesnet:libyang:yangre;"
80 "prefix re;"
81 "leaf pattern {"
82 " type string {";
83static const char *module_invertmatch = " { modifier invert-match; }";
84static const char *module_match = ";";
85static const char *module_end = "}}}";
86
87static int
88add_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
109int
110main(int argc, char* argv[])
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;
127 const struct lys_module *mod;
128 FILE *infile = NULL;
129 size_t len = 0;
130 ssize_t l;
Radek Krejci5819f7c2019-05-31 14:53:29 +0200131
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
155 while((l = getline(&str, &len, infile)) != -1) {
156 if (!blankline && str[0] == '\n') {
157 /* blank line */
158 blankline = 1;
159 continue;
160 }
161 if (str[0] != '\n' && str[l - 1] == '\n') {
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,
171 str[0] == ' ' ? &str[1] : str)) {
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
242 for (modstr = (char*)module_start, i = 0; i < patterns_count; i++) {
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 Vasko3a41dff2020-07-15 14:30:28 +0200266 if (lys_parse_mem(ctx, modstr, LYS_IN_YANG, &mod) || !mod->compiled || !mod->compiled->data) {
Radek Krejci5819f7c2019-05-31 14:53:29 +0200267 goto cleanup;
268 }
269
Radek Krejcid7857262020-11-06 10:29:22 +0100270 /* check the value */
271 match = lys_value_validate(ctx, mod->compiled->data, str, strlen(str));
272
Radek Krejci5819f7c2019-05-31 14:53:29 +0200273 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 Krejcid7857262020-11-06 10:29:22 +0100284 fprintf(stdout, "result : error (%s)\n", ly_errmsg(ctx));
Radek Krejci5819f7c2019-05-31 14:53:29 +0200285 }
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
295cleanup:
296 ly_ctx_destroy(ctx, NULL);
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 Krejci5819f7c2019-05-31 14:53:29 +0200307
308 return ret;
309}