Rebuilt win.amd64 against r3658 (new compiler, xp64/w2k3 not support any more; new...
[kbuild-mirror.git] / src / oldsed / testsuite / tst-pcre.c
blobc86509155002c6d240bf5612bd1adf9f1452ab7b
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.
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, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
21 #include "config.h"
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>
32 int
33 main (int argc, char **argv)
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;
47 #ifdef HAVE_MCHECK_H
48 mtrace ();
49 #endif
51 if (argc < 2)
53 fprintf (stderr, "Missing test filename\n");
54 return 1;
57 f = fopen (argv[1], "r");
58 if (f == NULL)
60 fprintf (stderr, "Couldn't open %s\n", argv[1]);
61 return 1;
64 if ((len = getline (&line, &line_len, f)) <= 0
65 || strncmp (line, "# PCRE", 6) != 0)
67 fprintf (stderr, "Not a PCRE test file\n");
68 fclose (f);
69 free (line);
70 return 1;
73 linenum = 1;
75 while ((len = getline (&line, &line_len, f)) > 0)
77 char *p;
78 unsigned long num;
80 ++linenum;
82 if (line[len - 1] == '\n')
83 line[--len] = '\0';
85 if (line[0] == '#')
86 continue;
88 if (line[0] == '\0')
90 /* End of test. */
91 ignorecase = 0;
92 pattern_valid = 0;
93 rm_valid = 0;
94 continue;
97 if (line[0] == '/')
99 /* Pattern. */
100 p = strrchr (line + 1, '/');
102 pattern_valid = 0;
103 rm_valid = 0;
104 if (p == NULL)
106 printf ("%lu: Invalid pattern line: %s\n", linenum, line);
107 ret = 1;
108 continue;
111 if (p[1] == 'i' && p[2] == '\0')
112 ignorecase = 1;
113 else if (p[1] != '\0')
115 printf ("%lu: Invalid pattern line: %s\n", linenum, line);
116 ret = 1;
117 continue;
120 if (pattern_alloced < (size_t) (p - line))
122 pattern = realloc (pattern, p - line);
123 if (pattern == NULL)
125 printf ("%lu: Cannot record pattern: %m\n", linenum);
126 ret = 1;
127 break;
129 pattern_alloced = p - line;
132 memcpy (pattern, line + 1, p - line - 1);
133 pattern[p - line - 1] = '\0';
134 pattern_valid = 1;
135 continue;
138 if (strncmp (line, " ", 4) == 0)
140 regex_t re;
141 int n;
143 if (!pattern_valid)
145 printf ("%lu: No previous valid pattern %s\n", linenum, line);
146 continue;
149 if (string_alloced < (size_t) (len - 3))
151 string = realloc (string, len - 3);
152 if (string == NULL)
154 printf ("%lu: Cannot record search string: %m\n", linenum);
155 ret = 1;
156 break;
158 string_alloced = len - 3;
161 memcpy (string, line + 4, len - 3);
163 n = regcomp (&re, pattern,
164 REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
165 if (n != 0)
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;
175 if (regexec (&re, string, 20, rm, 0))
177 rm[0].rm_so = -1;
178 rm[0].rm_eo = -1;
181 regfree (&re);
182 rm_valid = 1;
183 continue;
186 if (!rm_valid)
188 printf ("%lu: No preceeding pattern or search string\n", linenum);
189 ret = 1;
190 continue;
193 if (strcmp (line, "No match") == 0)
195 if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
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;
202 continue;
205 p = line;
206 if (*p == ' ')
207 ++p;
209 num = strtoul (p, &p, 10);
210 if (num >= 20 || *p != ':' || p[1] != ' ')
212 printf ("%lu: Invalid line %s\n", linenum, line);
213 ret = 1;
214 continue;
217 if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
219 if (strcmp (p + 2, "<unset>") != 0)
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;
226 continue;
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)
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;
242 free (pattern);
243 free (string);
244 free (line);
245 fclose (f);
246 return ret;