Tizen 2.1 base
[external/device-mapper.git] / unit-tests / regex / matcher_t.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
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.
10  *
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
14  */
15
16 #include "libdevmapper.h"
17 #include "log.h"
18
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/mman.h>
27
28
29 static int _read_spec(const char *file, char ***regex, int *nregex)
30 {
31         char buffer[1024], *start, *ptr;
32         FILE *fp = fopen(file, "r");
33         int asize = 100;
34         char **rx = dm_malloc(sizeof(*rx) * asize);
35         int nr = 0;
36
37         if (!fp)
38                 return 0;
39
40         while (fgets(buffer, sizeof(buffer),fp)) {
41
42                 /* trim leading whitespace */
43                 for (ptr = buffer; *ptr && isspace((int) *ptr); ptr++);
44
45                 if (!*ptr || *ptr == '#')
46                         continue;
47
48                 if (*ptr == '\"') {
49                         ptr++;
50                         start = ptr;
51                         while (*ptr && *ptr != '\"') {
52                                 if (*ptr == '\\')
53                                         ptr++;
54                                 ptr++;
55                         }
56
57                         if (!*ptr) {
58                                 fprintf(stderr, "Formatting error : "
59                                         "No terminating quote\n");
60                                 return 0;
61                         }
62
63                         rx[nr] = dm_malloc((ptr - start) + 1);
64                         strncpy(rx[nr], start, ptr - start);
65                         rx[nr][ptr - start] = '\0';
66                         nr++;
67                 } else {
68                         fprintf(stderr, "%s", ptr);
69                         fprintf(stderr, "Formatting error : \"<regex>\" "
70                                 "<token_name>\n");
71                         return 0;
72                 }
73         }
74
75         *regex = rx;
76         *nregex = nr;
77         return 1;
78 }
79
80 static void _free_regex(char **regex, int nregex)
81 {
82         int i;
83         for (i = 0; i < nregex; i++)
84                 dm_free(regex[i]);
85
86         dm_free(regex);
87 }
88
89 static void _scan_input(struct dm_regex *m, char **regex)
90 {
91         char buffer[256], *ptr;
92         int r;
93
94         while (fgets(buffer, sizeof(buffer), stdin)) {
95                 if ((ptr = strchr(buffer, '\n')))
96                         *ptr = '\0';
97
98                 r = dm_regex_match(m, buffer);
99
100                 if (r >= 0)
101                         printf("%s : %s\n", buffer, regex[r]);
102         }
103 }
104
105 int main(int argc, char **argv)
106 {
107         struct dm_pool *mem;
108         struct dm_regex *scanner;
109         char **regex;
110         int nregex;
111         int ret = 0;
112         int want_finger_print = 0, i;
113         const char *pattern_file = NULL;
114
115         for (i = 1; i < argc; i++)
116                 if (!strcmp(argv[i], "--fingerprint"))
117                         want_finger_print = 1;
118
119                 else
120                         pattern_file = argv[i];
121
122         if (!pattern_file) {
123                 fprintf(stderr, "Usage : %s [--fingerprint] <pattern_file>\n", argv[0]);
124                 exit(1);
125         }
126
127         dm_log_init_verbose(_LOG_DEBUG);
128
129         if (!(mem = dm_pool_create("match_regex", 10 * 1024))) {
130                 fprintf(stderr, "Couldn't create pool\n");
131                 ret = 2;
132                 goto err;
133         }
134
135         if (!_read_spec(pattern_file, &regex, &nregex)) {
136                 fprintf(stderr, "Couldn't read the lex specification\n");
137                 ret = 3;
138                 goto err;
139         }
140
141         if (!(scanner = dm_regex_create(mem, (const char **)regex, nregex))) {
142                 fprintf(stderr, "Couldn't build the lexer\n");
143                 ret = 4;
144                 goto err;
145         }
146
147         if (want_finger_print)
148                 printf("fingerprint: %x\n", dm_regex_fingerprint(scanner));
149         _scan_input(scanner, regex);
150         _free_regex(regex, nregex);
151
152     err:
153         dm_pool_destroy(mem);
154
155         return ret;
156 }