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., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #include <sys/types.h>
|
---|
22 | #include <mcheck.h>
|
---|
23 | #include <regex.h>
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <string.h>
|
---|
27 |
|
---|
28 | int
|
---|
29 | main (int argc, char **argv)
|
---|
30 | {
|
---|
31 | int ret = 0;
|
---|
32 | char *line = NULL;
|
---|
33 | size_t line_len = 0;
|
---|
34 | ssize_t len;
|
---|
35 | FILE *f;
|
---|
36 | char *pattern = NULL, *string = NULL;
|
---|
37 | regmatch_t rm[20];
|
---|
38 | size_t pattern_alloced = 0, string_alloced = 0;
|
---|
39 | int ignorecase = 0;
|
---|
40 | int pattern_valid = 0, rm_valid = 0;
|
---|
41 | size_t linenum;
|
---|
42 |
|
---|
43 | mtrace ();
|
---|
44 |
|
---|
45 | if (argc < 2)
|
---|
46 | {
|
---|
47 | fprintf (stderr, "Missing test filename\n");
|
---|
48 | return 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | f = fopen (argv[1], "r");
|
---|
52 | if (f == NULL)
|
---|
53 | {
|
---|
54 | fprintf (stderr, "Couldn't open %s\n", argv[1]);
|
---|
55 | return 1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if ((len = getline (&line, &line_len, f)) <= 0
|
---|
59 | || strncmp (line, "# PCRE", 6) != 0)
|
---|
60 | {
|
---|
61 | fprintf (stderr, "Not a PCRE test file\n");
|
---|
62 | fclose (f);
|
---|
63 | free (line);
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | linenum = 1;
|
---|
68 |
|
---|
69 | while ((len = getline (&line, &line_len, f)) > 0)
|
---|
70 | {
|
---|
71 | char *p;
|
---|
72 | unsigned long num;
|
---|
73 |
|
---|
74 | ++linenum;
|
---|
75 |
|
---|
76 | if (line[len - 1] == '\n')
|
---|
77 | line[--len] = '\0';
|
---|
78 |
|
---|
79 | if (line[0] == '#')
|
---|
80 | continue;
|
---|
81 |
|
---|
82 | if (line[0] == '\0')
|
---|
83 | {
|
---|
84 | /* End of test. */
|
---|
85 | ignorecase = 0;
|
---|
86 | pattern_valid = 0;
|
---|
87 | rm_valid = 0;
|
---|
88 | continue;
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (line[0] == '/')
|
---|
92 | {
|
---|
93 | /* Pattern. */
|
---|
94 | p = strrchr (line + 1, '/');
|
---|
95 |
|
---|
96 | pattern_valid = 0;
|
---|
97 | rm_valid = 0;
|
---|
98 | if (p == NULL)
|
---|
99 | {
|
---|
100 | printf ("%zd: Invalid pattern line: %s\n", linenum, line);
|
---|
101 | ret = 1;
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (p[1] == 'i' && p[2] == '\0')
|
---|
106 | ignorecase = 1;
|
---|
107 | else if (p[1] != '\0')
|
---|
108 | {
|
---|
109 | printf ("%zd: Invalid pattern line: %s\n", linenum, line);
|
---|
110 | ret = 1;
|
---|
111 | continue;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (pattern_alloced < (size_t) (p - line))
|
---|
115 | {
|
---|
116 | pattern = realloc (pattern, p - line);
|
---|
117 | if (pattern == NULL)
|
---|
118 | {
|
---|
119 | printf ("%zd: Cannot record pattern: %m\n", linenum);
|
---|
120 | ret = 1;
|
---|
121 | break;
|
---|
122 | }
|
---|
123 | pattern_alloced = p - line;
|
---|
124 | }
|
---|
125 |
|
---|
126 | memcpy (pattern, line + 1, p - line - 1);
|
---|
127 | pattern[p - line - 1] = '\0';
|
---|
128 | pattern_valid = 1;
|
---|
129 | continue;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (strncmp (line, " ", 4) == 0)
|
---|
133 | {
|
---|
134 | regex_t re;
|
---|
135 | int n;
|
---|
136 |
|
---|
137 | if (!pattern_valid)
|
---|
138 | {
|
---|
139 | printf ("%zd: No previous valid pattern %s\n", linenum, line);
|
---|
140 | continue;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (string_alloced < (size_t) (len - 3))
|
---|
144 | {
|
---|
145 | string = realloc (string, len - 3);
|
---|
146 | if (string == NULL)
|
---|
147 | {
|
---|
148 | printf ("%zd: Cannot record search string: %m\n", linenum);
|
---|
149 | ret = 1;
|
---|
150 | break;
|
---|
151 | }
|
---|
152 | string_alloced = len - 3;
|
---|
153 | }
|
---|
154 |
|
---|
155 | memcpy (string, line + 4, len - 3);
|
---|
156 |
|
---|
157 | n = regcomp (&re, pattern,
|
---|
158 | REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
|
---|
159 | if (n != 0)
|
---|
160 | {
|
---|
161 | char buf[500];
|
---|
162 | regerror (n, &re, buf, sizeof (buf));
|
---|
163 | printf ("%zd: regcomp failed for %s: %s\n",
|
---|
164 | linenum, pattern, buf);
|
---|
165 | ret = 1;
|
---|
166 | continue;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (regexec (&re, string, 20, rm, 0))
|
---|
170 | {
|
---|
171 | rm[0].rm_so = -1;
|
---|
172 | rm[0].rm_eo = -1;
|
---|
173 | }
|
---|
174 |
|
---|
175 | regfree (&re);
|
---|
176 | rm_valid = 1;
|
---|
177 | continue;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (!rm_valid)
|
---|
181 | {
|
---|
182 | printf ("%zd: No preceeding pattern or search string\n", linenum);
|
---|
183 | ret = 1;
|
---|
184 | continue;
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (strcmp (line, "No match") == 0)
|
---|
188 | {
|
---|
189 | if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
|
---|
190 | {
|
---|
191 | printf ("%zd: /%s/ on %s unexpectedly matched %d..%d\n",
|
---|
192 | linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
|
---|
193 | ret = 1;
|
---|
194 | }
|
---|
195 |
|
---|
196 | continue;
|
---|
197 | }
|
---|
198 |
|
---|
199 | p = line;
|
---|
200 | if (*p == ' ')
|
---|
201 | ++p;
|
---|
202 |
|
---|
203 | num = strtoul (p, &p, 10);
|
---|
204 | if (num >= 20 || *p != ':' || p[1] != ' ')
|
---|
205 | {
|
---|
206 | printf ("%zd: Invalid line %s\n", linenum, line);
|
---|
207 | ret = 1;
|
---|
208 | continue;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
|
---|
212 | {
|
---|
213 | if (strcmp (p + 2, "<unset>") != 0)
|
---|
214 | {
|
---|
215 | printf ("%zd: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
|
---|
216 | linenum, pattern, string, num,
|
---|
217 | rm[num].rm_so, rm[num].rm_eo);
|
---|
218 | ret = 1;
|
---|
219 | }
|
---|
220 | continue;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (rm[num].rm_eo < rm[num].rm_so
|
---|
224 | || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
|
---|
225 | || strncmp (p + 2, string + rm[num].rm_so,
|
---|
226 | rm[num].rm_eo - rm[num].rm_so) != 0)
|
---|
227 | {
|
---|
228 | printf ("%zd: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
|
---|
229 | linenum, pattern, string, p + 2, num,
|
---|
230 | rm[num].rm_so, rm[num].rm_eo);
|
---|
231 | ret = 1;
|
---|
232 | continue;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | free (pattern);
|
---|
237 | free (string);
|
---|
238 | free (line);
|
---|
239 | fclose (f);
|
---|
240 | return ret;
|
---|
241 | }
|
---|