]>
git.saurik.com Git - wxWidgets.git/blob - src/regex/re_main.c
14 int copts
= REG_EXTENDED
;
16 regoff_t startoff
= 0;
21 extern void regprint();
24 - main - do the simple case, hand off to regress() for regression
44 while ((c
= getopt(argc
, argv
, "c:e:S:E:x")) != EOF
)
46 case 'c': /* compile options */
47 copts
= options('c', optarg
);
49 case 'e': /* execute options */
50 eopts
= options('e', optarg
);
52 case 'S': /* start offset */
53 startoff
= (regoff_t
)atoi(optarg
);
55 case 'E': /* end offset */
56 endoff
= (regoff_t
)atoi(optarg
);
58 case 'x': /* Debugging. */
67 fprintf(stderr
, "usage: %s ", progname
);
68 fprintf(stderr
, "[-c copt][-C][-d] [re]\n");
77 err
= regcomp(&re
, argv
[optind
++], copts
);
79 len
= regerror(err
, &re
, erbuf
, sizeof(erbuf
));
80 fprintf(stderr
, "error %s, %d/%d `%s'\n",
81 eprint(err
), len
, sizeof(erbuf
), erbuf
);
84 regprint(&re
, stdout
);
91 if (eopts
®_STARTEND
) {
92 subs
[0].rm_so
= startoff
;
93 subs
[0].rm_eo
= strlen(argv
[optind
]) - endoff
;
95 err
= regexec(&re
, argv
[optind
], (size_t)NS
, subs
, eopts
);
97 len
= regerror(err
, &re
, erbuf
, sizeof(erbuf
));
98 fprintf(stderr
, "error %s, %d/%d `%s'\n",
99 eprint(err
), len
, sizeof(erbuf
), erbuf
);
102 if (!(copts
®_NOSUB
)) {
103 len
= (int)(subs
[0].rm_eo
- subs
[0].rm_so
);
104 if (subs
[0].rm_so
!= -1) {
106 printf("match `%.*s'\n", len
,
107 argv
[optind
] + subs
[0].rm_so
);
109 printf("match `'@%.1s\n",
110 argv
[optind
] + subs
[0].rm_so
);
112 for (i
= 1; i
< NS
; i
++)
113 if (subs
[i
].rm_so
!= -1)
114 printf("(%d) `%.*s'\n", i
,
115 (int)(subs
[i
].rm_eo
- subs
[i
].rm_so
),
116 argv
[optind
] + subs
[i
].rm_so
);
122 - regress - main loop of regression test
123 == void regress(FILE *in);
136 char *badpat
= "invalid regular expression";
138 char *bpname
= "REG_BADPAT";
141 while (fgets(inbuf
, sizeof(inbuf
), in
) != NULL
) {
143 if (inbuf
[0] == '#' || inbuf
[0] == '\n')
144 continue; /* NOTE CONTINUE */
145 inbuf
[strlen(inbuf
)-1] = '\0'; /* get rid of stupid \n */
147 fprintf(stdout
, "%d:\n", line
);
148 nf
= split(inbuf
, f
, MAXF
, "\t\t");
150 fprintf(stderr
, "bad input, line %d\n", line
);
153 for (i
= 0; i
< nf
; i
++)
154 if (strcmp(f
[i
], "\"\"") == 0)
160 try(f
[0], f
[1], f
[2], f
[3], f
[4], options('c', f
[1]));
161 if (opt('&', f
[1])) /* try with either type of RE */
162 try(f
[0], f
[1], f
[2], f
[3], f
[4],
163 options('c', f
[1]) &~ REG_EXTENDED
);
166 ne
= regerror(REG_BADPAT
, (regex_t
*)NULL
, erbuf
, sizeof(erbuf
));
167 if (strcmp(erbuf
, badpat
) != 0 || ne
!= strlen(badpat
)+1) {
168 fprintf(stderr
, "end: regerror() test gave `%s' not `%s'\n",
172 ne
= regerror(REG_BADPAT
, (regex_t
*)NULL
, erbuf
, (size_t)SHORT
);
173 if (strncmp(erbuf
, badpat
, SHORT
-1) != 0 || erbuf
[SHORT
-1] != '\0' ||
174 ne
!= strlen(badpat
)+1) {
175 fprintf(stderr
, "end: regerror() short test gave `%s' not `%.*s'\n",
176 erbuf
, SHORT
-1, badpat
);
179 ne
= regerror(REG_ITOA
|REG_BADPAT
, (regex_t
*)NULL
, erbuf
, sizeof(erbuf
));
180 if (strcmp(erbuf
, bpname
) != 0 || ne
!= strlen(bpname
)+1) {
181 fprintf(stderr
, "end: regerror() ITOA test gave `%s' not `%s'\n",
186 ne
= regerror(REG_ATOI
, &re
, erbuf
, sizeof(erbuf
));
187 if (atoi(erbuf
) != (int)REG_BADPAT
) {
188 fprintf(stderr
, "end: regerror() ATOI test gave `%s' not `%ld'\n",
189 erbuf
, (long)REG_BADPAT
);
191 } else if (ne
!= strlen(erbuf
)+1) {
192 fprintf(stderr
, "end: regerror() ATOI test len(`%s') = %ld\n",
193 erbuf
, (long)REG_BADPAT
);
199 - try - try it, and report on problems
200 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
203 try(f0
, f1
, f2
, f3
, f4
, opts
)
209 int opts
; /* may not match f1 */
213 regmatch_t subs
[NSUBS
];
215 char *should
[NSHOULD
];
220 char *type
= (opts
& REG_EXTENDED
) ? "ERE" : "BRE";
227 re
.re_endp
= (opts
®_PEND
) ? f0copy
+ strlen(f0copy
) : NULL
;
229 err
= regcomp(&re
, f0copy
, opts
);
230 if (err
!= 0 && (!opt('C', f1
) || err
!= efind(f2
))) {
231 /* unexpected error or wrong error */
232 len
= regerror(err
, &re
, erbuf
, sizeof(erbuf
));
233 fprintf(stderr
, "%d: %s error %s, %d/%d `%s'\n",
234 line
, type
, eprint(err
), len
,
235 sizeof(erbuf
), erbuf
);
237 } else if (err
== 0 && opt('C', f1
)) {
238 /* unexpected success */
239 fprintf(stderr
, "%d: %s should have given REG_%s\n",
242 err
= 1; /* so we won't try regexec */
253 if (options('e', f1
)®_STARTEND
) {
254 if (strchr(f2
, '(') == NULL
|| strchr(f2
, ')') == NULL
)
255 fprintf(stderr
, "%d: bad STARTEND syntax\n", line
);
256 subs
[0].rm_so
= strchr(f2
, '(') - f2
+ 1;
257 subs
[0].rm_eo
= strchr(f2
, ')') - f2
;
259 err
= regexec(&re
, f2copy
, NSUBS
, subs
, options('e', f1
));
261 if (err
!= 0 && (f3
!= NULL
|| err
!= REG_NOMATCH
)) {
262 /* unexpected error or wrong error */
263 len
= regerror(err
, &re
, erbuf
, sizeof(erbuf
));
264 fprintf(stderr
, "%d: %s exec error %s, %d/%d `%s'\n",
265 line
, type
, eprint(err
), len
,
266 sizeof(erbuf
), erbuf
);
268 } else if (err
!= 0) {
269 /* nothing more to check */
270 } else if (f3
== NULL
) {
271 /* unexpected success */
272 fprintf(stderr
, "%d: %s exec should have failed\n",
275 err
= 1; /* just on principle */
276 } else if (opts
®_NOSUB
) {
277 /* nothing more to check */
278 } else if ((grump
= check(f2
, subs
[0], f3
)) != NULL
) {
279 fprintf(stderr
, "%d: %s %s\n", line
, type
, grump
);
284 if (err
!= 0 || f4
== NULL
) {
289 for (i
= 1; i
< NSHOULD
; i
++)
291 nshould
= split(f4
, should
+1, NSHOULD
-1, ",");
296 for (i
= 1; i
< NSUBS
; i
++) {
297 grump
= check(f2
, subs
[i
], should
[i
]);
299 fprintf(stderr
, "%d: %s $%d %s\n", line
,
310 - options - pick options out of a regression-test string
311 == int options(int type, char *s);
315 int type
; /* 'c' compile, 'e' exec */
319 register int o
= (type
== 'c') ? copts
: eopts
;
320 register char *legal
= (type
== 'c') ? "bisnmp" : "^$#tl";
322 for (p
= s
; *p
!= '\0'; p
++)
323 if (strchr(legal
, *p
) != NULL
)
353 case 't': /* trace */
356 case 'l': /* force long representation */
359 case 'r': /* force backref use */
367 - opt - is a particular option in a regression string?
368 == int opt(int c, char *s);
375 return(strchr(s
, c
) != NULL
);
379 - fixstr - transform magic characters in strings
380 == void fixstr(register char *p);
389 for (; *p
!= '\0'; p
++)
401 - check - check a substring match
402 == char *check(char *str, regmatch_t sub, char *should);
404 char * /* NULL or complaint */
405 check(str
, sub
, should
)
413 static char grump
[500];
414 register char *at
= NULL
;
416 if (should
!= NULL
&& strcmp(should
, "-") == 0)
418 if (should
!= NULL
&& should
[0] == '@') {
423 /* check rm_so and rm_eo for consistency */
424 if (sub
.rm_so
> sub
.rm_eo
|| (sub
.rm_so
== -1 && sub
.rm_eo
!= -1) ||
425 (sub
.rm_so
!= -1 && sub
.rm_eo
== -1) ||
426 (sub
.rm_so
!= -1 && sub
.rm_so
< 0) ||
427 (sub
.rm_eo
!= -1 && sub
.rm_eo
< 0) ) {
428 sprintf(grump
, "start %ld end %ld", (long)sub
.rm_so
,
433 /* check for no match */
434 if (sub
.rm_so
== -1 && should
== NULL
)
437 return("did not match");
439 /* check for in range */
440 if (sub
.rm_eo
> strlen(str
)) {
441 sprintf(grump
, "start %ld end %ld, past end of string",
442 (long)sub
.rm_so
, (long)sub
.rm_eo
);
446 len
= (int)(sub
.rm_eo
- sub
.rm_so
);
447 shlen
= (int)strlen(should
);
450 /* check for not supposed to match */
451 if (should
== NULL
) {
452 sprintf(grump
, "matched `%.*s'", len
, p
);
456 /* check for wrong match */
457 if (len
!= shlen
|| strncmp(p
, should
, (size_t)shlen
) != 0) {
458 sprintf(grump
, "matched `%.*s' instead", len
, p
);
464 /* check null match in right place */
469 shlen
= 1; /* force check for end-of-string */
470 if (strncmp(p
, at
, shlen
) != 0) {
471 sprintf(grump
, "matched null at `%.20s'", p
);
478 - eprint - convert error number to name
479 == static char *eprint(int err);
485 static char epbuf
[100];
488 len
= regerror(REG_ITOA
|err
, (regex_t
*)NULL
, epbuf
, sizeof(epbuf
));
489 assert(len
<= sizeof(epbuf
));
494 - efind - convert error name to number
495 == static int efind(char *name);
501 static char efbuf
[100];
505 sprintf(efbuf
, "REG_%s", name
);
506 assert(strlen(efbuf
) < sizeof(efbuf
));
508 (void) regerror(REG_ATOI
, &re
, efbuf
, sizeof(efbuf
));