1 /* Regular expression tests.
2 Copyright (C) 2003-2022 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>
27 main (int argc
, char **argv
)
34 char *pattern
= NULL
, *string
= NULL
;
36 size_t pattern_alloced
= 0, string_alloced
= 0;
38 int pattern_valid
= 0, rm_valid
= 0;
45 fprintf (stderr
, "Missing test filename\n");
49 f
= fopen (argv
[1], "r");
52 fprintf (stderr
, "Couldn't open %s\n", argv
[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");
67 while ((len
= getline (&line
, &line_len
, f
)) > 0)
74 if (line
[len
- 1] == '\n')
92 p
= strrchr (line
+ 1, '/');
98 printf ("%zd: Invalid pattern line: %s\n", linenum
, line
);
103 if (p
[1] == 'i' && p
[2] == '\0')
105 else if (p
[1] != '\0')
107 printf ("%zd: Invalid pattern line: %s\n", linenum
, line
);
112 if (pattern_alloced
< (size_t) (p
- line
))
114 pattern
= realloc (pattern
, p
- line
);
117 printf ("%zd: Cannot record pattern: %m\n", linenum
);
121 pattern_alloced
= p
- line
;
124 memcpy (pattern
, line
+ 1, p
- line
- 1);
125 pattern
[p
- line
- 1] = '\0';
130 if (strncmp (line
, " ", 4) == 0)
137 printf ("%zd: No previous valid pattern %s\n", linenum
, line
);
141 if (string_alloced
< (size_t) (len
- 3))
143 string
= realloc (string
, len
- 3);
146 printf ("%zd: Cannot record search string: %m\n", linenum
);
150 string_alloced
= len
- 3;
153 memcpy (string
, line
+ 4, len
- 3);
155 n
= regcomp (&re
, pattern
,
156 REG_EXTENDED
| (ignorecase
? REG_ICASE
: 0));
160 regerror (n
, &re
, buf
, sizeof (buf
));
161 printf ("%zd: regcomp failed for %s: %s\n",
162 linenum
, pattern
, buf
);
167 if (regexec (&re
, string
, 20, rm
, 0))
180 printf ("%zd: No preceeding pattern or search string\n", linenum
);
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
);
201 num
= strtoul (p
, &p
, 10);
202 if (num
>= 20 || *p
!= ':' || p
[1] != ' ')
204 printf ("%zd: Invalid line %s\n", linenum
, line
);
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
);
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
);