aboutsummaryrefslogtreecommitdiff
blob: f6c0ea4fa10cb06454f3890886bfd9992dfc7222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Regular expression tests.
   Copyright (C) 2003-2015 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <sys/types.h>
#include <mcheck.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main (int argc, char **argv)
{
  int ret = 0;
  char *line = NULL;
  size_t line_len = 0;
  ssize_t len;
  FILE *f;
  char *pattern = NULL, *string = NULL;
  regmatch_t rm[20];
  size_t pattern_alloced = 0, string_alloced = 0;
  int ignorecase = 0;
  int pattern_valid = 0, rm_valid = 0;
  size_t linenum;

  mtrace ();

  if (argc < 2)
    {
      fprintf (stderr, "Missing test filename\n");
      return 1;
    }

  f = fopen (argv[1], "r");
  if (f == NULL)
    {
      fprintf (stderr, "Couldn't open %s\n", argv[1]);
      return 1;
    }

  if ((len = getline (&line, &line_len, f)) <= 0
      || strncmp (line, "# PCRE", 6) != 0)
    {
      fprintf (stderr, "Not a PCRE test file\n");
      fclose (f);
      free (line);
      return 1;
    }

  linenum = 1;

  while ((len = getline (&line, &line_len, f)) > 0)
    {
      char *p;
      unsigned long num;

      ++linenum;

      if (line[len - 1] == '\n')
	line[--len] = '\0';

      if (line[0] == '#')
	continue;

      if (line[0] == '\0')
	{
	  /* End of test.  */
	  ignorecase = 0;
	  pattern_valid = 0;
	  rm_valid = 0;
	  continue;
	}

      if (line[0] == '/')
	{
	  /* Pattern.  */
	  p = strrchr (line + 1, '/');

	  pattern_valid = 0;
	  rm_valid = 0;
	  if (p == NULL)
	    {
	      printf ("%zd: Invalid pattern line: %s\n", linenum, line);
	      ret = 1;
	      continue;
	    }

	  if (p[1] == 'i' && p[2] == '\0')
	    ignorecase = 1;
	  else if (p[1] != '\0')
	    {
	      printf ("%zd: Invalid pattern line: %s\n", linenum, line);
	      ret = 1;
	      continue;
	    }

	  if (pattern_alloced < (size_t) (p - line))
	    {
	      pattern = realloc (pattern, p - line);
	      if (pattern == NULL)
		{
		  printf ("%zd: Cannot record pattern: %m\n", linenum);
		  ret = 1;
		  break;
		}
	      pattern_alloced = p - line;
	    }

	  memcpy (pattern, line + 1, p - line - 1);
	  pattern[p - line - 1] = '\0';
	  pattern_valid = 1;
	  continue;
	}

      if (strncmp (line, "    ", 4) == 0)
	{
	  regex_t re;
	  int n;

	  if (!pattern_valid)
	    {
	      printf ("%zd: No previous valid pattern %s\n", linenum, line);
	      continue;
	    }

	  if (string_alloced < (size_t) (len - 3))
	    {
	      string = realloc (string, len - 3);
	      if (string == NULL)
		{
		  printf ("%zd: Cannot record search string: %m\n", linenum);
		  ret = 1;
		  break;
		}
	      string_alloced = len - 3;
	    }

	  memcpy (string, line + 4, len - 3);

	  n = regcomp (&re, pattern,
		       REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
	  if (n != 0)
	    {
	      char buf[500];
	      regerror (n, &re, buf, sizeof (buf));
	      printf ("%zd: regcomp failed for %s: %s\n",
		      linenum, pattern, buf);
	      ret = 1;
	      continue;
	    }

	  if (regexec (&re, string, 20, rm, 0))
	    {
	      rm[0].rm_so = -1;
	      rm[0].rm_eo = -1;
	    }

	  regfree (&re);
	  rm_valid = 1;
	  continue;
	}

      if (!rm_valid)
	{
	  printf ("%zd: No preceeding pattern or search string\n", linenum);
	  ret = 1;
	  continue;
	}

      if (strcmp (line, "No match") == 0)
	{
	  if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
	    {
	      printf ("%zd: /%s/ on %s unexpectedly matched %d..%d\n",
		      linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
	      ret = 1;
	    }

	  continue;
	}

      p = line;
      if (*p == ' ')
        ++p;

      num = strtoul (p, &p, 10);
      if (num >= 20 || *p != ':' || p[1] != ' ')
	{
	  printf ("%zd: Invalid line %s\n", linenum, line);
	  ret = 1;
	  continue;
	}

      if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
	{
	  if (strcmp (p + 2, "<unset>") != 0)
	    {
	      printf ("%zd: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
		      linenum, pattern, string, num,
		      rm[num].rm_so, rm[num].rm_eo);
	      ret = 1;
	    }
	  continue;
	}

      if (rm[num].rm_eo < rm[num].rm_so
	  || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
	  || strncmp (p + 2, string + rm[num].rm_so,
		      rm[num].rm_eo - rm[num].rm_so) != 0)
	{
	  printf ("%zd: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
		  linenum, pattern, string, p + 2, num,
		  rm[num].rm_so, rm[num].rm_eo);
	  ret = 1;
	  continue;
	}
    }

  free (pattern);
  free (string);
  free (line);
  fclose (f);
  return ret;
}