m68k: use _dl_static_init to set GLR0(dl_pagesize)
[glibc.git] / posix / tst-pcre.c
blobafae926699e95558116ad8a93244b75d1a02068e
1 /* Regular expression tests.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
21 #include <mcheck.h>
22 #include <regex.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 int
28 main (int argc, char **argv)
30 int ret = 0;
31 char *line = NULL;
32 size_t line_len = 0;
33 ssize_t len;
34 FILE *f;
35 char *pattern = NULL, *string = NULL;
36 regmatch_t rm[20];
37 size_t pattern_alloced = 0, string_alloced = 0;
38 int ignorecase = 0;
39 int pattern_valid = 0, rm_valid = 0;
40 size_t linenum;
42 mtrace ();
44 if (argc < 2)
46 fprintf (stderr, "Missing test filename\n");
47 return 1;
50 f = fopen (argv[1], "r");
51 if (f == NULL)
53 fprintf (stderr, "Couldn't open %s\n", argv[1]);
54 return 1;
57 if ((len = getline (&line, &line_len, f)) <= 0
58 || strncmp (line, "# PCRE", 6) != 0)
60 fprintf (stderr, "Not a PCRE test file\n");
61 fclose (f);
62 free (line);
63 return 1;
66 linenum = 1;
68 while ((len = getline (&line, &line_len, f)) > 0)
70 char *p;
71 unsigned long num;
73 ++linenum;
75 if (line[len - 1] == '\n')
76 line[--len] = '\0';
78 if (line[0] == '#')
79 continue;
81 if (line[0] == '\0')
83 /* End of test. */
84 ignorecase = 0;
85 pattern_valid = 0;
86 rm_valid = 0;
87 continue;
90 if (line[0] == '/')
92 /* Pattern. */
93 p = strrchr (line + 1, '/');
95 pattern_valid = 0;
96 rm_valid = 0;
97 if (p == NULL)
99 printf ("%zd: Invalid pattern line: %s\n", linenum, line);
100 ret = 1;
101 continue;
104 if (p[1] == 'i' && p[2] == '\0')
105 ignorecase = 1;
106 else if (p[1] != '\0')
108 printf ("%zd: Invalid pattern line: %s\n", linenum, line);
109 ret = 1;
110 continue;
113 if (pattern_alloced < (size_t) (p - line))
115 pattern = realloc (pattern, p - line);
116 if (pattern == NULL)
118 printf ("%zd: Cannot record pattern: %m\n", linenum);
119 ret = 1;
120 break;
122 pattern_alloced = p - line;
125 memcpy (pattern, line + 1, p - line - 1);
126 pattern[p - line - 1] = '\0';
127 pattern_valid = 1;
128 continue;
131 if (strncmp (line, " ", 4) == 0)
133 regex_t re;
134 int n;
136 if (!pattern_valid)
138 printf ("%zd: No previous valid pattern %s\n", linenum, line);
139 continue;
142 if (string_alloced < (size_t) (len - 3))
144 string = realloc (string, len - 3);
145 if (string == NULL)
147 printf ("%zd: Cannot record search string: %m\n", linenum);
148 ret = 1;
149 break;
151 string_alloced = len - 3;
154 memcpy (string, line + 4, len - 3);
156 n = regcomp (&re, pattern,
157 REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
158 if (n != 0)
160 char buf[500];
161 regerror (n, &re, buf, sizeof (buf));
162 printf ("%zd: regcomp failed for %s: %s\n",
163 linenum, pattern, buf);
164 ret = 1;
165 continue;
168 if (regexec (&re, string, 20, rm, 0))
170 rm[0].rm_so = -1;
171 rm[0].rm_eo = -1;
174 regfree (&re);
175 rm_valid = 1;
176 continue;
179 if (!rm_valid)
181 printf ("%zd: No preceeding pattern or search string\n", linenum);
182 ret = 1;
183 continue;
186 if (strcmp (line, "No match") == 0)
188 if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
190 printf ("%zd: /%s/ on %s unexpectedly matched %d..%d\n",
191 linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
192 ret = 1;
195 continue;
198 p = line;
199 if (*p == ' ')
200 ++p;
202 num = strtoul (p, &p, 10);
203 if (num >= 20 || *p != ':' || p[1] != ' ')
205 printf ("%zd: Invalid line %s\n", linenum, line);
206 ret = 1;
207 continue;
210 if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
212 if (strcmp (p + 2, "<unset>") != 0)
214 printf ("%zd: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
215 linenum, pattern, string, num,
216 rm[num].rm_so, rm[num].rm_eo);
217 ret = 1;
219 continue;
222 if (rm[num].rm_eo < rm[num].rm_so
223 || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
224 || strncmp (p + 2, string + rm[num].rm_so,
225 rm[num].rm_eo - rm[num].rm_so) != 0)
227 printf ("%zd: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
228 linenum, pattern, string, p + 2, num,
229 rm[num].rm_so, rm[num].rm_eo);
230 ret = 1;
231 continue;
235 free (pattern);
236 free (string);
237 free (line);
238 fclose (f);
239 return ret;