1 | /* Regular expression tests.
|
---|
2 | Copyright (C) 2003 Free Software Foundation, Inc.
|
---|
3 | This file is part of the GNU C Library.
|
---|
4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
|
---|
5 |
|
---|
6 | The GNU C Library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Lesser General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2.1 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with the GNU C Library; if not, write to the Free
|
---|
18 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
19 | 02110-1301 USA. */
|
---|
20 |
|
---|
21 | #include "config.h"
|
---|
22 |
|
---|
23 | #include <sys/types.h>
|
---|
24 | #ifdef HAVE_MCHECK_H
|
---|
25 | #include <mcheck.h>
|
---|
26 | #endif
|
---|
27 | #include <regex.h>
|
---|
28 | #include <stdio.h>
|
---|
29 | #include <stdlib.h>
|
---|
30 | #include <string.h>
|
---|
31 |
|
---|
32 | int
|
---|
33 | main (int argc, char **argv)
|
---|
34 | {
|
---|
35 | int ret = 0;
|
---|
36 | char *line = NULL;
|
---|
37 | size_t line_len = 0;
|
---|
38 | ssize_t len;
|
---|
39 | FILE *f;
|
---|
40 | char *pattern = NULL, *string = NULL;
|
---|
41 | regmatch_t rm[20];
|
---|
42 | size_t pattern_alloced = 0, string_alloced = 0;
|
---|
43 | int ignorecase = 0;
|
---|
44 | int pattern_valid = 0, rm_valid = 0;
|
---|
45 | size_t linenum;
|
---|
46 |
|
---|
47 | #ifdef HAVE_MCHECK_H
|
---|
48 | mtrace ();
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | if (argc < 2)
|
---|
52 | {
|
---|
53 | fprintf (stderr, "Missing test filename\n");
|
---|
54 | return 1;
|
---|
55 | }
|
---|
56 |
|
---|
57 | f = fopen (argv[1], "r");
|
---|
58 | if (f == NULL)
|
---|
59 | {
|
---|
60 | fprintf (stderr, "Couldn't open %s\n", argv[1]);
|
---|
61 | return 1;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if ((len = getline (&line, &line_len, f)) <= 0
|
---|
65 | || strncmp (line, "# PCRE", 6) != 0)
|
---|
66 | {
|
---|
67 | fprintf (stderr, "Not a PCRE test file\n");
|
---|
68 | fclose (f);
|
---|
69 | free (line);
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | linenum = 1;
|
---|
74 |
|
---|
75 | while ((len = getline (&line, &line_len, f)) > 0)
|
---|
76 | {
|
---|
77 | char *p;
|
---|
78 | unsigned long num;
|
---|
79 |
|
---|
80 | ++linenum;
|
---|
81 |
|
---|
82 | if (line[len - 1] == '\n')
|
---|
83 | line[--len] = '\0';
|
---|
84 |
|
---|
85 | if (line[0] == '#')
|
---|
86 | continue;
|
---|
87 |
|
---|
88 | if (line[0] == '\0')
|
---|
89 | {
|
---|
90 | /* End of test. */
|
---|
91 | ignorecase = 0;
|
---|
92 | pattern_valid = 0;
|
---|
93 | rm_valid = 0;
|
---|
94 | continue;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (line[0] == '/')
|
---|
98 | {
|
---|
99 | /* Pattern. */
|
---|
100 | p = strrchr (line + 1, '/');
|
---|
101 |
|
---|
102 | pattern_valid = 0;
|
---|
103 | rm_valid = 0;
|
---|
104 | if (p == NULL)
|
---|
105 | {
|
---|
106 | printf ("%lu: Invalid pattern line: %s\n", linenum, line);
|
---|
107 | ret = 1;
|
---|
108 | continue;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (p[1] == 'i' && p[2] == '\0')
|
---|
112 | ignorecase = 1;
|
---|
113 | else if (p[1] != '\0')
|
---|
114 | {
|
---|
115 | printf ("%lu: Invalid pattern line: %s\n", linenum, line);
|
---|
116 | ret = 1;
|
---|
117 | continue;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (pattern_alloced < (size_t) (p - line))
|
---|
121 | {
|
---|
122 | pattern = realloc (pattern, p - line);
|
---|
123 | if (pattern == NULL)
|
---|
124 | {
|
---|
125 | printf ("%lu: Cannot record pattern: %m\n", linenum);
|
---|
126 | ret = 1;
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | pattern_alloced = p - line;
|
---|
130 | }
|
---|
131 |
|
---|
132 | memcpy (pattern, line + 1, p - line - 1);
|
---|
133 | pattern[p - line - 1] = '\0';
|
---|
134 | pattern_valid = 1;
|
---|
135 | continue;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (strncmp (line, " ", 4) == 0)
|
---|
139 | {
|
---|
140 | regex_t re;
|
---|
141 | int n;
|
---|
142 |
|
---|
143 | if (!pattern_valid)
|
---|
144 | {
|
---|
145 | printf ("%lu: No previous valid pattern %s\n", linenum, line);
|
---|
146 | continue;
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (string_alloced < (size_t) (len - 3))
|
---|
150 | {
|
---|
151 | string = realloc (string, len - 3);
|
---|
152 | if (string == NULL)
|
---|
153 | {
|
---|
154 | printf ("%lu: Cannot record search string: %m\n", linenum);
|
---|
155 | ret = 1;
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | string_alloced = len - 3;
|
---|
159 | }
|
---|
160 |
|
---|
161 | memcpy (string, line + 4, len - 3);
|
---|
162 |
|
---|
163 | n = regcomp (&re, pattern,
|
---|
164 | REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
|
---|
165 | if (n != 0)
|
---|
166 | {
|
---|
167 | char buf[500];
|
---|
168 | regerror (n, &re, buf, sizeof (buf));
|
---|
169 | printf ("%lu: regcomp failed for %s: %s\n",
|
---|
170 | linenum, pattern, buf);
|
---|
171 | ret = 1;
|
---|
172 | continue;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (regexec (&re, string, 20, rm, 0))
|
---|
176 | {
|
---|
177 | rm[0].rm_so = -1;
|
---|
178 | rm[0].rm_eo = -1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | regfree (&re);
|
---|
182 | rm_valid = 1;
|
---|
183 | continue;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (!rm_valid)
|
---|
187 | {
|
---|
188 | printf ("%lu: No preceeding pattern or search string\n", linenum);
|
---|
189 | ret = 1;
|
---|
190 | continue;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (strcmp (line, "No match") == 0)
|
---|
194 | {
|
---|
195 | if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
|
---|
196 | {
|
---|
197 | printf ("%lu: /%s/ on %s unexpectedly matched %d..%d\n",
|
---|
198 | linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
|
---|
199 | ret = 1;
|
---|
200 | }
|
---|
201 |
|
---|
202 | continue;
|
---|
203 | }
|
---|
204 |
|
---|
205 | p = line;
|
---|
206 | if (*p == ' ')
|
---|
207 | ++p;
|
---|
208 |
|
---|
209 | num = strtoul (p, &p, 10);
|
---|
210 | if (num >= 20 || *p != ':' || p[1] != ' ')
|
---|
211 | {
|
---|
212 | printf ("%lu: Invalid line %s\n", linenum, line);
|
---|
213 | ret = 1;
|
---|
214 | continue;
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
|
---|
218 | {
|
---|
219 | if (strcmp (p + 2, "<unset>") != 0)
|
---|
220 | {
|
---|
221 | printf ("%lu: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
|
---|
222 | linenum, pattern, string, num,
|
---|
223 | rm[num].rm_so, rm[num].rm_eo);
|
---|
224 | ret = 1;
|
---|
225 | }
|
---|
226 | continue;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (rm[num].rm_eo < rm[num].rm_so
|
---|
230 | || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
|
---|
231 | || strncmp (p + 2, string + rm[num].rm_so,
|
---|
232 | rm[num].rm_eo - rm[num].rm_so) != 0)
|
---|
233 | {
|
---|
234 | printf ("%lu: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
|
---|
235 | linenum, pattern, string, p + 2, num,
|
---|
236 | rm[num].rm_so, rm[num].rm_eo);
|
---|
237 | ret = 1;
|
---|
238 | continue;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | free (pattern);
|
---|
243 | free (string);
|
---|
244 | free (line);
|
---|
245 | fclose (f);
|
---|
246 | return ret;
|
---|
247 | }
|
---|