8 #include "tests_include.h"
15 int copts = MY_REG_EXTENDED;
17 my_regoff_t startoff = 0;
18 my_regoff_t endoff = 0;
21 extern int split(
char *
string,
char *fields[],
int nfields,
char *sep);
31 int getopt(
int argc,
char *argv[],
const char *optstring)
43 if (*(opt+1) && *(opt+2))
71 char *input_file_name= NULL;
77 while ((c = getopt(argc, argv,
"c:e:i:S:E:xI")) != EOF)
80 copts = options(
'c', optarg);
83 eopts = options(
'e', optarg);
86 input_file_name= optarg;
89 startoff = (my_regoff_t)atoi(optarg);
92 endoff = (my_regoff_t)atoi(optarg);
106 fprintf(stderr,
"usage: %s ", progname);
108 "[-c copt][-e eopt][-i filename][-S][-E][-x][-I] [re]\n");
117 if (optind >= argc && !input_file_name) {
122 if (input_file_name) {
123 FILE *input_file= fopen(input_file_name,
"r");
125 fprintf(stderr,
"Could not open '%s' : ", input_file_name);
134 err = my_regcomp(&re, argv[optind++], copts, &my_charset_latin1);
136 len = my_regerror(err, &re, erbuf,
sizeof(erbuf));
137 fprintf(stderr,
"error %s, %d/%d `%s'\n",
138 eprint(err), (
int) len, (
int)
sizeof(erbuf), erbuf);
141 regprint(&re, stdout);
143 if (optind >= argc) {
148 if (eopts&MY_REG_STARTEND) {
149 subs[0].rm_so = startoff;
150 subs[0].rm_eo = strlen(argv[optind]) - endoff;
152 err = my_regexec(&re, argv[optind], (
size_t)NS, subs, eopts);
154 len = my_regerror(err, &re, erbuf,
sizeof(erbuf));
155 fprintf(stderr,
"error %s, %d/%d `%s'\n",
156 eprint(err), (
int) len, (
int)
sizeof(erbuf), erbuf);
159 if (!(copts&MY_REG_NOSUB)) {
160 len = (int)(subs[0].rm_eo - subs[0].rm_so);
161 if (subs[0].rm_so != -1) {
163 printf(
"match `%.*s'\n", (
int)len,
164 argv[optind] + subs[0].rm_so);
166 printf(
"match `'@%.1s\n",
167 argv[optind] + subs[0].rm_so);
169 for (i = 1; i < NS; i++)
170 if (subs[i].rm_so != -1)
171 printf(
"(%d) `%.*s'\n", i,
172 (
int)(subs[i].rm_eo - subs[i].rm_so),
173 argv[optind] + subs[i].rm_so);
179 get_next_line(s,
size, stream)
185 return fgets(s, size, stream);
186 if (test_array[line])
187 return strncpy(s, test_array[line], size);
208 const char *badpat =
"invalid regular expression";
210 const char *bpname =
"MY_REG_BADPAT";
213 while (get_next_line(inbuf,
sizeof(inbuf), in) != NULL) {
215 if (inbuf[0] ==
'#' || inbuf[0] ==
'\n' || inbuf[0] ==
'\0')
217 if (inbuf[strlen(inbuf)-1] ==
'\n')
218 inbuf[strlen(inbuf)-1] =
'\0';
220 fprintf(stdout,
"%d: <%s>\n", line, inbuf);
221 nf = split(inbuf, f, MAXF, (
char*)
"\t\t");
223 fprintf(stderr,
"bad input, line %d\n", line);
226 for (i = 0; i < nf; i++)
227 if (strcmp(f[i],
"\"\"") == 0)
233 rx_try(f[0], f[1], f[2], f[3], f[4], options(
'c', f[1]));
235 rx_try(f[0], f[1], f[2], f[3], f[4],
236 options(
'c', f[1]) &~ MY_REG_EXTENDED);
239 ne = my_regerror(MY_REG_BADPAT, (
my_regex_t *)NULL, erbuf,
sizeof(erbuf));
240 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
241 fprintf(stderr,
"end: regerror() test gave `%s' not `%s'\n",
245 ne = my_regerror(MY_REG_BADPAT, (
my_regex_t *)NULL, erbuf, (
size_t)SHORT);
246 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] !=
'\0' ||
247 ne != strlen(badpat)+1) {
248 fprintf(stderr,
"end: regerror() short test gave `%s' not `%.*s'\n",
249 erbuf, SHORT-1, badpat);
252 ne = my_regerror(MY_REG_ITOA|MY_REG_BADPAT, (
my_regex_t *)NULL, erbuf,
sizeof(erbuf));
253 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
254 fprintf(stderr,
"end: regerror() ITOA test gave `%s' not `%s'\n",
259 ne = my_regerror(MY_REG_ATOI, &re, erbuf,
sizeof(erbuf));
260 if (atoi(erbuf) != (
int)MY_REG_BADPAT) {
261 fprintf(stderr,
"end: regerror() ATOI test gave `%s' not `%ld'\n",
262 erbuf, (
long)MY_REG_BADPAT);
264 }
else if (ne != strlen(erbuf)+1) {
265 fprintf(stderr,
"end: regerror() ATOI test len(`%s') = %ld\n",
266 erbuf, (
long)MY_REG_BADPAT);
276 rx_try(f0, f1, f2, f3, f4, opts)
288 char *should[NSHOULD];
293 const char *
type = (opts & MY_REG_EXTENDED) ?
"ERE" :
"BRE";
300 re.re_endp = (opts&MY_REG_PEND) ? f0copy + strlen(f0copy) : NULL;
302 err = my_regcomp(&re, f0copy, opts, &my_charset_latin1);
303 if (err != 0 && (!opt(
'C', f1) || err != efind(f2))) {
305 len = my_regerror(err, &re, erbuf,
sizeof(erbuf));
306 fprintf(stderr,
"%d: %s error %s, %d/%d `%s'\n",
307 line, type, eprint(err), len,
308 (
int)
sizeof(erbuf), erbuf);
310 }
else if (err == 0 && opt(
'C', f1)) {
312 fprintf(stderr,
"%d: %s should have given MY_REG_%s\n",
326 if (options(
'e', f1)&MY_REG_STARTEND) {
327 if (strchr(f2,
'(') == NULL || strchr(f2,
')') == NULL)
328 fprintf(stderr,
"%d: bad STARTEND syntax\n", line);
329 subs[0].rm_so = strchr(f2,
'(') - f2 + 1;
330 subs[0].rm_eo = strchr(f2,
')') - f2;
332 err = my_regexec(&re, f2copy, NSUBS, subs, options(
'e', f1));
334 if (err != 0 && (f3 != NULL || err != MY_REG_NOMATCH)) {
336 len = my_regerror(err, &re, erbuf,
sizeof(erbuf));
337 fprintf(stderr,
"%d: %s exec error %s, %d/%d `%s'\n",
338 line, type, eprint(err), len,
339 (
int)
sizeof(erbuf), erbuf);
341 }
else if (err != 0) {
343 }
else if (f3 == NULL) {
345 fprintf(stderr,
"%d: %s exec should have failed\n",
349 }
else if (opts&MY_REG_NOSUB) {
351 }
else if ((grump = check(f2, subs[0], f3)) != NULL) {
352 fprintf(stderr,
"%d: %s %s\n", line, type, grump);
357 if (err != 0 || f4 == NULL) {
362 for (i = 1; i < NSHOULD; i++)
364 nshould = split(f4, should+1, NSHOULD-1, (
char*)
",");
367 should[1] = (
char*)
"";
369 for (i = 1; i < NSUBS; i++) {
370 grump = check(f2, subs[i], should[i]);
372 fprintf(stderr,
"%d: %s $%d %s\n", line,
392 register int o = (type ==
'c') ? copts : eopts;
393 register const char *legal = (type ==
'c') ?
"bisnmp" :
"^$#tl";
395 for (p = s; *p !=
'\0'; p++)
396 if (strchr(legal, *p) != NULL)
399 o &= ~MY_REG_EXTENDED;
411 o &= ~MY_REG_EXTENDED;
424 o |= MY_REG_STARTEND;
448 return(strchr(s, c) != NULL);
462 for (; *p !=
'\0'; p++)
478 check(str, sub, should)
486 static char grump[500];
487 register char *at = NULL;
489 if (should != NULL && strcmp(should,
"-") == 0)
491 if (should != NULL && should[0] ==
'@') {
497 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
498 (sub.rm_so != -1 && sub.rm_eo == -1) ||
499 (sub.rm_so != -1 && sub.rm_so < 0) ||
500 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
501 sprintf(grump,
"start %ld end %ld", (
long)sub.rm_so,
507 if (sub.rm_so == -1 && should == NULL)
510 return((
char*)
"did not match");
513 if ((
int) sub.rm_eo > (
int) strlen(str)) {
514 sprintf(grump,
"start %ld end %ld, past end of string",
515 (
long)sub.rm_so, (
long)sub.rm_eo);
519 len = (int)(sub.rm_eo - sub.rm_so);
520 shlen = (int)strlen(should);
524 if (should == NULL) {
525 sprintf(grump,
"matched `%.*s'", len, p);
530 if (len != shlen || strncmp(p, should, (
size_t)shlen) != 0) {
531 sprintf(grump,
"matched `%.*s' instead", len, p);
543 if (strncmp(p, at, shlen) != 0) {
544 sprintf(grump,
"matched null at `%.20s'", p);
558 static char epbuf[100];
561 len = my_regerror(MY_REG_ITOA|err, (
my_regex_t *)NULL, epbuf,
sizeof(epbuf));
562 assert(len <=
sizeof(epbuf));
574 static char efbuf[100];
577 sprintf(efbuf,
"MY_REG_%s", name);
578 assert(strlen(efbuf) <
sizeof(efbuf));
580 (void) my_regerror(MY_REG_ATOI, &re, efbuf,
sizeof(efbuf));