2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU General Public License v.2.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #include "libdevmapper.h"
22 #include <sys/types.h>
29 static int _read_spec(const char *file, char ***regex, int *nregex)
31 char buffer[1024], *start, *ptr;
32 FILE *fp = fopen(file, "r");
34 char **rx = dm_malloc(sizeof(*rx) * asize);
40 while (fgets(buffer, sizeof(buffer),fp)) {
42 /* trim leading whitespace */
43 for (ptr = buffer; *ptr && isspace((int) *ptr); ptr++);
45 if (!*ptr || *ptr == '#')
51 while (*ptr && *ptr != '\"') {
58 fprintf(stderr, "Formatting error : "
59 "No terminating quote\n");
63 rx[nr] = dm_malloc((ptr - start) + 1);
64 strncpy(rx[nr], start, ptr - start);
65 rx[nr][ptr - start] = '\0';
68 fprintf(stderr, "%s", ptr);
69 fprintf(stderr, "Formatting error : \"<regex>\" "
80 static void _free_regex(char **regex, int nregex)
83 for (i = 0; i < nregex; i++)
89 static void _scan_input(struct dm_regex *m, char **regex)
91 char buffer[256], *ptr;
94 while (fgets(buffer, sizeof(buffer), stdin)) {
95 if ((ptr = strchr(buffer, '\n')))
98 r = dm_regex_match(m, buffer);
101 printf("%s : %s\n", buffer, regex[r]);
105 int main(int argc, char **argv)
108 struct dm_regex *scanner;
112 int want_finger_print = 0, i;
113 const char *pattern_file = NULL;
115 for (i = 1; i < argc; i++)
116 if (!strcmp(argv[i], "--fingerprint"))
117 want_finger_print = 1;
120 pattern_file = argv[i];
123 fprintf(stderr, "Usage : %s [--fingerprint] <pattern_file>\n", argv[0]);
127 dm_log_init_verbose(_LOG_DEBUG);
129 if (!(mem = dm_pool_create("match_regex", 10 * 1024))) {
130 fprintf(stderr, "Couldn't create pool\n");
135 if (!_read_spec(pattern_file, ®ex, &nregex)) {
136 fprintf(stderr, "Couldn't read the lex specification\n");
141 if (!(scanner = dm_regex_create(mem, (const char **)regex, nregex))) {
142 fprintf(stderr, "Couldn't build the lexer\n");
147 if (want_finger_print)
148 printf("fingerprint: %x\n", dm_regex_fingerprint(scanner));
149 _scan_input(scanner, regex);
150 _free_regex(regex, nregex);
153 dm_pool_destroy(mem);