build-many-glibcs.py: Add openrisc hard float glibc variant
[glibc.git] / posix / tst-pcre.c
blob453759b451531e3324f6533acf8cb25711d9684e
1 /* Regular expression tests.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
20 #include <mcheck.h>
21 #include <regex.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 int
27 main (int argc, char **argv)
29 int ret = 0;
30 char *line = NULL;
31 size_t line_len = 0;
32 ssize_t len;
33 FILE *f;
34 char *pattern = NULL, *string = NULL;
35 regmatch_t rm[20];
36 size_t pattern_alloced = 0, string_alloced = 0;
37 int ignorecase = 0;
38 int pattern_valid = 0, rm_valid = 0;
39 size_t linenum;
41 mtrace ();
43 if (argc < 2)
45 fprintf (stderr, "Missing test filename\n");
46 return 1;
49 f = fopen (argv[1], "r");
50 if (f == NULL)
52 fprintf (stderr, "Couldn't open %s\n", argv[1]);
53 return 1;
56 if ((len = getline (&line, &line_len, f)) <= 0
57 || strncmp (line, "# PCRE", 6) != 0)
59 fprintf (stderr, "Not a PCRE test file\n");
60 fclose (f);
61 free (line);
62 return 1;
65 linenum = 1;
67 while ((len = getline (&line, &line_len, f)) > 0)
69 char *p;
70 unsigned long num;
72 ++linenum;
74 if (line[len - 1] == '\n')
75 line[--len] = '\0';
77 if (line[0] == '#')
78 continue;
80 if (line[0] == '\0')
82 /* End of test. */
83 ignorecase = 0;
84 pattern_valid = 0;
85 rm_valid = 0;
86 continue;
89 if (line[0] == '/')
91 /* Pattern. */
92 p = strrchr (line + 1, '/');
94 pattern_valid = 0;
95 rm_valid = 0;
96 if (p == NULL)
98 printf ("%zd: Invalid pattern line: %s\n", linenum, line);
99 ret = 1;
100 continue;
103 if (p[1] == 'i' && p[2] == '\0')
104 ignorecase = 1;
105 else if (p[1] != '\0')
107 printf ("%zd: Invalid pattern line: %s\n", linenum, line);
108 ret = 1;
109 continue;
112 if (pattern_alloced < (size_t) (p - line))
114 pattern = realloc (pattern, p - line);
115 if (pattern == NULL)
117 printf ("%zd: Cannot record pattern: %m\n", linenum);
118 ret = 1;
119 break;
121 pattern_alloced = p - line;
124 memcpy (pattern, line + 1, p - line - 1);
125 pattern[p - line - 1] = '\0';
126 pattern_valid = 1;
127 continue;
130 if (strncmp (line, " ", 4) == 0)
132 regex_t re;
133 int n;
135 if (!pattern_valid)
137 printf ("%zd: No previous valid pattern %s\n", linenum, line);
138 continue;
141 if (string_alloced < (size_t) (len - 3))
143 string = realloc (string, len - 3);
144 if (string == NULL)
146 printf ("%zd: Cannot record search string: %m\n", linenum);
147 ret = 1;
148 break;
150 string_alloced = len - 3;
153 memcpy (string, line + 4, len - 3);
155 n = regcomp (&re, pattern,
156 REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
157 if (n != 0)
159 char buf[500];
160 regerror (n, &re, buf, sizeof (buf));
161 printf ("%zd: regcomp failed for %s: %s\n",
162 linenum, pattern, buf);
163 ret = 1;
164 continue;
167 if (regexec (&re, string, 20, rm, 0))
169 rm[0].rm_so = -1;
170 rm[0].rm_eo = -1;
173 regfree (&re);
174 rm_valid = 1;
175 continue;
178 if (!rm_valid)
180 printf ("%zd: No preceding pattern or search string\n", linenum);
181 ret = 1;
182 continue;
185 if (strcmp (line, "No match") == 0)
187 if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
189 printf ("%zd: /%s/ on %s unexpectedly matched %d..%d\n",
190 linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
191 ret = 1;
194 continue;
197 p = line;
198 if (*p == ' ')
199 ++p;
201 num = strtoul (p, &p, 10);
202 if (num >= 20 || *p != ':' || p[1] != ' ')
204 printf ("%zd: Invalid line %s\n", linenum, line);
205 ret = 1;
206 continue;
209 if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
211 if (strcmp (p + 2, "<unset>") != 0)
213 printf ("%zd: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
214 linenum, pattern, string, num,
215 rm[num].rm_so, rm[num].rm_eo);
216 ret = 1;
218 continue;
221 if (rm[num].rm_eo < rm[num].rm_so
222 || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
223 || strncmp (p + 2, string + rm[num].rm_so,
224 rm[num].rm_eo - rm[num].rm_so) != 0)
226 printf ("%zd: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
227 linenum, pattern, string, p + 2, num,
228 rm[num].rm_so, rm[num].rm_eo);
229 ret = 1;
230 continue;
234 free (pattern);
235 free (string);
236 free (line);
237 fclose (f);
238 return ret;