1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <errno.h>
6#include <fcntl.h>
7#include <regex.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/stat.h>
12#include <unistd.h>
13
14int
15main (int argc, char *argv[])
16{
17  struct stat st;
18  static const char *pat[] = {
19    ".?.?.?.?.?.?.?argc",
20    "(.?)(.?)(.?)(.?)(.?)(.?)(.?)argc",
21    "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
22    "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
23    "((((((((((.?))))))))))argc" };
24
25  size_t len;
26  int fd;
27  int testno, i, j, k, l;
28  char *string;
29  char *buf;
30
31  if (argc < 2)
32    abort ();
33
34  fd = open (argv[1], O_RDONLY);
35  if (fd < 0)
36    {
37      printf ("Couldn't open %s: %s\n", argv[1], strerror (errno));
38      abort ();
39    }
40
41  if (fstat (fd, &st) < 0)
42    {
43      printf ("Couldn't fstat %s: %s\n", argv[1], strerror (errno));
44      abort ();
45    }
46
47  buf = malloc (st.st_size + 1);
48  if (buf == NULL)
49    {
50      printf ("Couldn't allocate buffer: %s\n", strerror (errno));
51      abort ();
52    }
53
54  if (read (fd, buf, st.st_size) != (ssize_t) st.st_size)
55    {
56      printf ("Couldn't read %s", argv[1]);
57      abort ();
58    }
59
60  close (fd);
61  buf[st.st_size] = '\0';
62
63  string = buf;
64  len = st.st_size;
65
66  for (testno = 0; testno < 4; ++testno)
67    for (i = 0; i < sizeof (pat) / sizeof (pat[0]); ++i)
68      {
69	regex_t rbuf;
70	struct re_pattern_buffer rpbuf;
71	int err;
72
73	printf ("test %d pattern %d", testno, i);
74	if (testno < 2)
75	  {
76	    err = regcomp (&rbuf, pat[i],
77			   REG_EXTENDED | (testno ? REG_NOSUB : 0));
78	    if (err != 0)
79	      {
80		char errstr[300];
81		putchar ('\n');
82		regerror (err, &rbuf, errstr, sizeof (errstr));
83		puts (errstr);
84		return err;
85	      }
86	  }
87	else
88	  {
89	    const char *s;
90	    re_set_syntax (RE_SYNTAX_POSIX_EGREP
91			   | (testno == 3 ? RE_NO_SUB : 0));
92
93	    memset (&rpbuf, 0, sizeof (rpbuf));
94	    s = re_compile_pattern (pat[i], strlen (pat[i]), &rpbuf);
95	    if (s != NULL)
96	      {
97		printf ("\n%s\n", s);
98		abort ();
99	      }
100
101	    /* Just so that this can be tested with earlier glibc as well.  */
102	    if (testno == 3)
103	      rpbuf.no_sub = 1;
104	  }
105
106      if (testno < 2)
107	{
108	  regmatch_t pmatch[71];
109	  err = regexec (&rbuf, string, 71, pmatch, 0);
110	  if (err == REG_NOMATCH)
111	    {
112	      puts ("\nregexec failed");
113	      abort ();
114	    }
115
116	  if (testno == 0)
117	    {
118	      if (pmatch[0].rm_eo != pmatch[0].rm_so + 11
119		  || pmatch[0].rm_eo > len
120		  || string + pmatch[0].rm_so >= strchr (string, 'R')
121		  || strncmp (string + pmatch[0].rm_so,
122			      "n (int argc",
123			      sizeof "n (int argc" - 1)
124		     != 0)
125		{
126		  puts ("\nregexec without REG_NOSUB did not find the correct match");
127		  abort ();
128		}
129
130	      if (i > 0)
131		for (j = 0, l = 1; j < 7; ++j)
132		  for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
133		    if (pmatch[l].rm_so != pmatch[0].rm_so + j
134			|| pmatch[l].rm_eo != pmatch[l].rm_so + 1)
135		      {
136			printf ("\npmatch[%d] incorrect\n", l);
137			abort ();
138		      }
139	    }
140	}
141      else
142	{
143	  struct re_registers regs;
144	  int match;
145
146	  memset (&regs, 0, sizeof (regs));
147	  match = re_search (&rpbuf, string, len, 0, len,
148				 &regs);
149	  if (match < 0)
150	    {
151	      puts ("\nre_search failed");
152	      abort ();
153	    }
154
155	  if (match + 11 > len
156	      || string + match >= strchr (string, 'R')
157	      || strncmp (string + match,
158			  "n (int argc",
159			  sizeof "n (int argc" - 1)
160		  != 0)
161	    {
162	      puts ("\nre_search did not find the correct match");
163	      abort ();
164	    }
165
166	  if (testno == 2)
167	    {
168	      if (regs.num_regs != 2 + (i == 0 ? 0 : i == 1 ? 7 : 70))
169		{
170		  printf ("\nincorrect num_regs %d\n", regs.num_regs);
171		  abort ();
172		}
173
174	      if (regs.start[0] != match || regs.end[0] != match + 11)
175		{
176		  printf ("\nincorrect regs.{start,end}[0] = { %d, %d}\n",
177			  regs.start[0], regs.end[0]);
178		  abort ();
179		}
180
181	      if (regs.start[regs.num_regs - 1] != -1
182		  || regs.end[regs.num_regs - 1] != -1)
183		{
184		  puts ("\nincorrect regs.{start,end}[num_regs - 1]");
185		  abort ();
186		}
187
188	      if (i > 0)
189		for (j = 0, l = 1; j < 7; ++j)
190		  for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
191		    if (regs.start[l] != match + j
192			|| regs.end[l] != regs.start[l] + 1)
193		      {
194			printf ("\nregs.{start,end}[%d] incorrect\n", l);
195			abort ();
196		      }
197	    }
198	}
199
200      putchar ('\n');
201
202      if (testno < 2)
203	regfree (&rbuf);
204      else
205	regfree (&rpbuf);
206    }
207
208  exit (0);
209}
210