1 /* Regular expression tests.
2    Copyright (C) 2003-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
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>
25 
26 int
main(int argc,char ** argv)27 main (int argc, char **argv)
28 {
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;
40 
41   mtrace ();
42 
43   if (argc < 2)
44     {
45       fprintf (stderr, "Missing test filename\n");
46       return 1;
47     }
48 
49   f = fopen (argv[1], "r");
50   if (f == NULL)
51     {
52       fprintf (stderr, "Couldn't open %s\n", argv[1]);
53       return 1;
54     }
55 
56   if ((len = getline (&line, &line_len, f)) <= 0
57       || strncmp (line, "# PCRE", 6) != 0)
58     {
59       fprintf (stderr, "Not a PCRE test file\n");
60       fclose (f);
61       free (line);
62       return 1;
63     }
64 
65   linenum = 1;
66 
67   while ((len = getline (&line, &line_len, f)) > 0)
68     {
69       char *p;
70       unsigned long num;
71 
72       ++linenum;
73 
74       if (line[len - 1] == '\n')
75 	line[--len] = '\0';
76 
77       if (line[0] == '#')
78 	continue;
79 
80       if (line[0] == '\0')
81 	{
82 	  /* End of test.  */
83 	  ignorecase = 0;
84 	  pattern_valid = 0;
85 	  rm_valid = 0;
86 	  continue;
87 	}
88 
89       if (line[0] == '/')
90 	{
91 	  /* Pattern.  */
92 	  p = strrchr (line + 1, '/');
93 
94 	  pattern_valid = 0;
95 	  rm_valid = 0;
96 	  if (p == NULL)
97 	    {
98 	      printf ("%zd: Invalid pattern line: %s\n", linenum, line);
99 	      ret = 1;
100 	      continue;
101 	    }
102 
103 	  if (p[1] == 'i' && p[2] == '\0')
104 	    ignorecase = 1;
105 	  else if (p[1] != '\0')
106 	    {
107 	      printf ("%zd: Invalid pattern line: %s\n", linenum, line);
108 	      ret = 1;
109 	      continue;
110 	    }
111 
112 	  if (pattern_alloced < (size_t) (p - line))
113 	    {
114 	      pattern = realloc (pattern, p - line);
115 	      if (pattern == NULL)
116 		{
117 		  printf ("%zd: Cannot record pattern: %m\n", linenum);
118 		  ret = 1;
119 		  break;
120 		}
121 	      pattern_alloced = p - line;
122 	    }
123 
124 	  memcpy (pattern, line + 1, p - line - 1);
125 	  pattern[p - line - 1] = '\0';
126 	  pattern_valid = 1;
127 	  continue;
128 	}
129 
130       if (strncmp (line, "    ", 4) == 0)
131 	{
132 	  regex_t re;
133 	  int n;
134 
135 	  if (!pattern_valid)
136 	    {
137 	      printf ("%zd: No previous valid pattern %s\n", linenum, line);
138 	      continue;
139 	    }
140 
141 	  if (string_alloced < (size_t) (len - 3))
142 	    {
143 	      string = realloc (string, len - 3);
144 	      if (string == NULL)
145 		{
146 		  printf ("%zd: Cannot record search string: %m\n", linenum);
147 		  ret = 1;
148 		  break;
149 		}
150 	      string_alloced = len - 3;
151 	    }
152 
153 	  memcpy (string, line + 4, len - 3);
154 
155 	  n = regcomp (&re, pattern,
156 		       REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
157 	  if (n != 0)
158 	    {
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;
165 	    }
166 
167 	  if (regexec (&re, string, 20, rm, 0))
168 	    {
169 	      rm[0].rm_so = -1;
170 	      rm[0].rm_eo = -1;
171 	    }
172 
173 	  regfree (&re);
174 	  rm_valid = 1;
175 	  continue;
176 	}
177 
178       if (!rm_valid)
179 	{
180 	  printf ("%zd: No preceeding pattern or search string\n", linenum);
181 	  ret = 1;
182 	  continue;
183 	}
184 
185       if (strcmp (line, "No match") == 0)
186 	{
187 	  if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
188 	    {
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;
192 	    }
193 
194 	  continue;
195 	}
196 
197       p = line;
198       if (*p == ' ')
199         ++p;
200 
201       num = strtoul (p, &p, 10);
202       if (num >= 20 || *p != ':' || p[1] != ' ')
203 	{
204 	  printf ("%zd: Invalid line %s\n", linenum, line);
205 	  ret = 1;
206 	  continue;
207 	}
208 
209       if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
210 	{
211 	  if (strcmp (p + 2, "<unset>") != 0)
212 	    {
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;
217 	    }
218 	  continue;
219 	}
220 
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)
225 	{
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;
231 	}
232     }
233 
234   free (pattern);
235   free (string);
236   free (line);
237   fclose (f);
238   return ret;
239 }
240