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