]> git.ipfire.org Git - thirdparty/glibc.git/blob - posix/tst-boost.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / posix / tst-boost.c
1 /* Regular expression tests.
2 Copyright (C) 2003-2014 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 void
28 frob_escapes (char *src, int pattern)
29 {
30 char *dst;
31
32 for (dst = src; *src != '\0'; dst++, src++)
33 {
34 if (*src == '\\')
35 {
36 switch (src[1])
37 {
38 case 't':
39 src++;
40 *dst = '\t';
41 continue;
42 case 'n':
43 src++;
44 *dst = '\n';
45 continue;
46 case 'r':
47 src++;
48 *dst = '\r';
49 continue;
50 case '\\':
51 case '^':
52 case '{':
53 case '|':
54 case '}':
55 if (!pattern)
56 {
57 src++;
58 *dst = *src;
59 continue;
60 }
61 break;
62 }
63 }
64 if (src != dst)
65 *dst = *src;
66 }
67 *dst = '\0';
68 }
69
70 int
71 main (int argc, char **argv)
72 {
73 int ret = 0, n;
74 char *line = NULL;
75 size_t line_len = 0;
76 ssize_t len;
77 FILE *f;
78 char *pattern, *string;
79 int flags = REG_EXTENDED;
80 int eflags = 0;
81 regex_t re;
82 regmatch_t rm[20];
83
84 mtrace ();
85
86 if (argc < 2)
87 {
88 fprintf (stderr, "Missing test filename\n");
89 return 1;
90 }
91
92 f = fopen (argv[1], "r");
93 if (f == NULL)
94 {
95 fprintf (stderr, "Couldn't open %s\n", argv[1]);
96 return 1;
97 }
98
99 while ((len = getline (&line, &line_len, f)) > 0)
100 {
101 char *p, *q;
102 int i;
103
104 if (line[len - 1] == '\n')
105 line[--len] = '\0';
106
107 puts (line);
108
109 if (line[0] == ';')
110 continue;
111
112 if (line[0] == '\0')
113 continue;
114
115 if (line[0] == '-')
116 {
117 if (strstr (line, "REG_BASIC"))
118 flags = 0;
119 else
120 flags = REG_EXTENDED;
121 if (strstr (line, "REG_ICASE"))
122 flags |= REG_ICASE;
123 if (strstr (line, "REG_NEWLINE"))
124 flags |= REG_NEWLINE;
125 eflags = 0;
126 if (strstr (line, "REG_NOTBOL"))
127 eflags |= REG_NOTBOL;
128 if (strstr (line, "REG_NOTEOL"))
129 eflags |= REG_NOTEOL;
130 continue;
131 }
132
133 pattern = line + strspn (line, " \t");
134 if (*pattern == '\0')
135 continue;
136 p = pattern + strcspn (pattern, " \t");
137 if (*p == '\0')
138 continue;
139 *p++ = '\0';
140
141 string = p + strspn (p, " \t");
142 if (*string == '\0')
143 continue;
144 if (*string == '"')
145 {
146 string++;
147 p = strchr (string, '"');
148 if (p == NULL)
149 continue;
150 *p++ = '\0';
151 }
152 else
153 {
154 p = string + strcspn (string, " \t");
155 if (*string == '!')
156 string = NULL;
157 else if (*p == '\0')
158 continue;
159 else
160 *p++ = '\0';
161 }
162
163 frob_escapes (pattern, 1);
164 if (string != NULL)
165 frob_escapes (string, 0);
166
167 n = regcomp (&re, pattern, flags);
168 if (n != 0)
169 {
170 if (string != NULL)
171 {
172 char buf[500];
173 regerror (n, &re, buf, sizeof (buf));
174 printf ("FAIL regcomp unexpectedly failed: %s\n",
175 buf);
176 ret = 1;
177 }
178 continue;
179 }
180 else if (string == NULL)
181 {
182 regfree (&re);
183 puts ("FAIL regcomp unpexpectedly succeeded");
184 ret = 1;
185 continue;
186 }
187
188 if (regexec (&re, string, 20, rm, eflags))
189 {
190 for (i = 0; i < 20; ++i)
191 {
192 rm[i].rm_so = -1;
193 rm[i].rm_eo = -1;
194 }
195 }
196
197 regfree (&re);
198
199 for (i = 0; i < 20 && *p != '\0'; ++i)
200 {
201 int rm_so, rm_eo;
202
203 rm_so = strtol (p, &q, 10);
204 if (p == q)
205 break;
206 p = q;
207
208 rm_eo = strtol (p, &q, 10);
209 if (p == q)
210 break;
211 p = q;
212
213 if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
214 {
215 printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
216 i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
217 ret = 1;
218 break;
219 }
220 }
221 }
222
223 free (line);
224 fclose (f);
225 return ret;
226 }