VirtualBox

source: kBuild/trunk/src/sed/testsuite/tst-boost.c@ 2421

Last change on this file since 2421 was 599, checked in by bird, 18 years ago

GNU sed 4.1.5.

File size: 4.4 KB
Line 
1/* Regular expression tests.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <[email protected]>, 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, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21#include "config.h"
22
23#include <sys/types.h>
24#ifdef HAVE_MCHECK_H
25#include <mcheck.h>
26#endif
27#include <regex.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32void
33frob_escapes (char *src, int pattern)
34{
35 char *dst;
36
37 for (dst = src; *src != '\0'; dst++, src++)
38 {
39 if (*src == '\\')
40 {
41 switch (src[1])
42 {
43 case 't':
44 src++;
45 *dst = '\t';
46 continue;
47 case 'n':
48 src++;
49 *dst = '\n';
50 continue;
51 case 'r':
52 src++;
53 *dst = '\r';
54 continue;
55 case '\\':
56 case '^':
57 case '{':
58 case '|':
59 case '}':
60 if (!pattern)
61 {
62 src++;
63 *dst = *src;
64 continue;
65 }
66 break;
67 }
68 }
69 if (src != dst)
70 *dst = *src;
71 }
72 *dst = '\0';
73}
74
75int
76main (int argc, char **argv)
77{
78 int ret = 0, n;
79 char *line = NULL;
80 size_t line_len = 0;
81 ssize_t len;
82 FILE *f;
83 char *pattern, *string;
84 int flags = REG_EXTENDED;
85 int eflags = 0;
86 regex_t re;
87 regmatch_t rm[20];
88
89#ifdef HAVE_MCHECK_H
90 mtrace ();
91#endif
92
93 if (argc < 2)
94 {
95 fprintf (stderr, "Missing test filename\n");
96 return 1;
97 }
98
99 f = fopen (argv[1], "r");
100 if (f == NULL)
101 {
102 fprintf (stderr, "Couldn't open %s\n", argv[1]);
103 return 1;
104 }
105
106 while ((len = getline (&line, &line_len, f)) > 0)
107 {
108 char *p, *q;
109 int i;
110
111 if (line[len - 1] == '\n')
112 line[--len] = '\0';
113
114 puts (line);
115
116 if (line[0] == ';')
117 continue;
118
119 if (line[0] == '\0')
120 continue;
121
122 if (line[0] == '-')
123 {
124 if (strstr (line, "REG_BASIC"))
125 flags = 0;
126 else
127 flags = REG_EXTENDED;
128 if (strstr (line, "REG_ICASE"))
129 flags |= REG_ICASE;
130 if (strstr (line, "REG_NEWLINE"))
131 flags |= REG_NEWLINE;
132 eflags = 0;
133 if (strstr (line, "REG_NOTBOL"))
134 eflags |= REG_NOTBOL;
135 if (strstr (line, "REG_NOTEOL"))
136 eflags |= REG_NOTEOL;
137 continue;
138 }
139
140 pattern = line + strspn (line, " \t");
141 if (*pattern == '\0')
142 continue;
143 p = pattern + strcspn (pattern, " \t");
144 if (*p == '\0')
145 continue;
146 *p++ = '\0';
147
148 string = p + strspn (p, " \t");
149 if (*string == '\0')
150 continue;
151 if (*string == '"')
152 {
153 string++;
154 p = strchr (string, '"');
155 if (p == NULL)
156 continue;
157 *p++ = '\0';
158 }
159 else
160 {
161 p = string + strcspn (string, " \t");
162 if (*string == '!')
163 string = NULL;
164 else if (*p == '\0')
165 continue;
166 else
167 *p++ = '\0';
168 }
169
170 frob_escapes (pattern, 1);
171 if (string != NULL)
172 frob_escapes (string, 0);
173
174 n = regcomp (&re, pattern, flags);
175 if (n != 0)
176 {
177 if (string != NULL)
178 {
179 char buf[500];
180 regerror (n, &re, buf, sizeof (buf));
181 printf ("FAIL regcomp unexpectedly failed: %s\n",
182 buf);
183 ret = 1;
184 }
185 continue;
186 }
187 else if (string == NULL)
188 {
189 regfree (&re);
190 puts ("FAIL regcomp unpexpectedly succeeded");
191 ret = 1;
192 continue;
193 }
194
195 if (regexec (&re, string, 20, rm, eflags))
196 {
197 for (i = 0; i < 20; ++i)
198 {
199 rm[i].rm_so = -1;
200 rm[i].rm_eo = -1;
201 }
202 }
203
204 regfree (&re);
205
206 for (i = 0; i < 20 && *p != '\0'; ++i)
207 {
208 int rm_so, rm_eo;
209
210 rm_so = strtol (p, &q, 10);
211 if (p == q)
212 break;
213 p = q;
214
215 rm_eo = strtol (p, &q, 10);
216 if (p == q)
217 break;
218 p = q;
219
220 if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
221 {
222 printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
223 i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
224 ret = 1;
225 break;
226 }
227 }
228 }
229
230 free (line);
231 fclose (f);
232 return ret;
233}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette