Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / posix / tst-pcre.c
1 /* Regular expression tests.
2    Copyright (C) 2003-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <sys/types.h>
21 #include <mcheck.h>
22 #include <regex.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 int
28 main (int argc, char **argv)
29 {
30   int ret = 0;
31   char *line = NULL;
32   size_t line_len = 0;
33   ssize_t len;
34   FILE *f;
35   char *pattern = NULL, *string = NULL;
36   regmatch_t rm[20];
37   size_t pattern_alloced = 0, string_alloced = 0;
38   int ignorecase = 0;
39   int pattern_valid = 0, rm_valid = 0;
40   size_t linenum;
41
42   mtrace ();
43
44   if (argc < 2)
45     {
46       fprintf (stderr, "Missing test filename\n");
47       return 1;
48     }
49
50   f = fopen (argv[1], "r");
51   if (f == NULL)
52     {
53       fprintf (stderr, "Couldn't open %s\n", argv[1]);
54       return 1;
55     }
56
57   if ((len = getline (&line, &line_len, f)) <= 0
58       || strncmp (line, "# PCRE", 6) != 0)
59     {
60       fprintf (stderr, "Not a PCRE test file\n");
61       fclose (f);
62       free (line);
63       return 1;
64     }
65
66   linenum = 1;
67
68   while ((len = getline (&line, &line_len, f)) > 0)
69     {
70       char *p;
71       unsigned long num;
72
73       ++linenum;
74
75       if (line[len - 1] == '\n')
76         line[--len] = '\0';
77
78       if (line[0] == '#')
79         continue;
80
81       if (line[0] == '\0')
82         {
83           /* End of test.  */
84           ignorecase = 0;
85           pattern_valid = 0;
86           rm_valid = 0;
87           continue;
88         }
89
90       if (line[0] == '/')
91         {
92           /* Pattern.  */
93           p = strrchr (line + 1, '/');
94
95           pattern_valid = 0;
96           rm_valid = 0;
97           if (p == NULL)
98             {
99               printf ("%zd: Invalid pattern line: %s\n", linenum, line);
100               ret = 1;
101               continue;
102             }
103
104           if (p[1] == 'i' && p[2] == '\0')
105             ignorecase = 1;
106           else if (p[1] != '\0')
107             {
108               printf ("%zd: Invalid pattern line: %s\n", linenum, line);
109               ret = 1;
110               continue;
111             }
112
113           if (pattern_alloced < (size_t) (p - line))
114             {
115               pattern = realloc (pattern, p - line);
116               if (pattern == NULL)
117                 {
118                   printf ("%zd: Cannot record pattern: %m\n", linenum);
119                   ret = 1;
120                   break;
121                 }
122               pattern_alloced = p - line;
123             }
124
125           memcpy (pattern, line + 1, p - line - 1);
126           pattern[p - line - 1] = '\0';
127           pattern_valid = 1;
128           continue;
129         }
130
131       if (strncmp (line, "    ", 4) == 0)
132         {
133           regex_t re;
134           int n;
135     
136           if (!pattern_valid)
137             {
138               printf ("%zd: No previous valid pattern %s\n", linenum, line);
139               continue;
140             }
141
142           if (string_alloced < (size_t) (len - 3))
143             {
144               string = realloc (string, len - 3);
145               if (string == NULL)
146                 {
147                   printf ("%zd: Cannot record search string: %m\n", linenum);
148                   ret = 1;
149                   break;
150                 }
151               string_alloced = len - 3;
152             }
153
154           memcpy (string, line + 4, len - 3);
155
156           n = regcomp (&re, pattern,
157                        REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
158           if (n != 0)
159             {
160               char buf[500];
161               regerror (n, &re, buf, sizeof (buf));
162               printf ("%zd: regcomp failed for %s: %s\n",
163                       linenum, pattern, buf);
164               ret = 1;
165               continue;
166             }
167
168           if (regexec (&re, string, 20, rm, 0))
169             {
170               rm[0].rm_so = -1;
171               rm[0].rm_eo = -1;
172             }
173
174           regfree (&re);
175           rm_valid = 1;
176           continue;
177         }
178
179       if (!rm_valid)
180         {
181           printf ("%zd: No preceeding pattern or search string\n", linenum);
182           ret = 1;
183           continue;
184         }
185
186       if (strcmp (line, "No match") == 0)
187         {
188           if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
189             {
190               printf ("%zd: /%s/ on %s unexpectedly matched %d..%d\n",
191                       linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
192               ret = 1;
193             }
194
195           continue;
196         }
197
198       p = line;
199       if (*p == ' ')
200         ++p;
201
202       num = strtoul (p, &p, 10);
203       if (num >= 20 || *p != ':' || p[1] != ' ')
204         {
205           printf ("%zd: Invalid line %s\n", linenum, line);
206           ret = 1;
207           continue;
208         }
209
210       if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
211         {
212           if (strcmp (p + 2, "<unset>") != 0)
213             {
214               printf ("%zd: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
215                       linenum, pattern, string, num,
216                       rm[num].rm_so, rm[num].rm_eo);
217               ret = 1;
218             }
219           continue;
220         }
221
222       if (rm[num].rm_eo < rm[num].rm_so
223           || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
224           || strncmp (p + 2, string + rm[num].rm_so,
225                       rm[num].rm_eo - rm[num].rm_so) != 0)
226         {
227           printf ("%zd: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
228                   linenum, pattern, string, p + 2, num,
229                   rm[num].rm_so, rm[num].rm_eo);
230           ret = 1;
231           continue;
232         }
233     }
234
235   free (pattern);
236   free (string);
237   free (line);
238   fclose (f);
239   return ret;
240 }