41 static void __relist_free_results(
REList *re_list)
49 re_list->
string = (
char *)NULL;
53 re_list->
offsets = (regmatch_t *)NULL;
54 re_list->
substrs = (
char **)NULL;
78 int re_compile(regex_t *preg,
const char *pattern,
int cflags)
83 errcode = regcomp(preg, pattern, cflags);
91 "Could not compile regular expression: '%s'\n"
92 " -> %s\n", pattern, errstr);
123 errlen = regerror(errcode, preg, NULL, 0);
127 "Could not create regular expression error message\n"
128 " -> regerror not implemented\n");
130 return((
char *)NULL);
135 errstr = (
char *)malloc(errlen *
sizeof(
char));
139 "Could not create regular expression error message\n"
140 " -> memory allocation error\n");
142 return((
char *)NULL);
145 regerror(errcode, preg, errstr, errlen);
179 errcode = regexec(preg,
string, nmatch, pmatch, eflags);
184 else if (errcode == REG_NOMATCH) {
193 "Could not execute regular expression for string: '%s'\n"
194 " -> %s\n",
string, errstr);
209 if (preg) regfree(preg);
223 for (si = 0; si < nmatch; si++) {
224 if (substrings[si]) free(substrings[si]);
253 substrings = (
char **)calloc(nmatch,
sizeof(
char *));
258 for (mi = 0; mi < nmatch; mi++) {
260 if (pmatch[mi].rm_so == -1) {
261 substrings[mi] = (
char *)NULL;
265 length = pmatch[mi].rm_eo - pmatch[mi].rm_so;
267 substrings[mi] = (
char *)malloc((length + 1) *
sizeof(char));
268 if (!substrings[mi]) {
272 substrings[mi][length] =
'\0';
275 strncpy(substrings[mi], (
string + pmatch[mi].rm_so), length);
285 for (mi--; mi > 0; mi--) {
286 if (substrings[mi]) free(substrings[mi]);
288 if (substrings[0]) free(substrings[0]);
293 "Could not extract subtrings from regular expression match for: '%s'\n"
294 " -> memory allocation error\n",
string);
296 return((
char **)NULL);
321 const char **patterns,
339 re_list = new_re_list;
345 new_nregs = re_list->
nregs + npatterns;
347 new_patterns = (
char **)realloc(
348 re_list->
patterns, new_nregs *
sizeof(
char *));
358 new_cflags = (
int *)realloc(
359 re_list->
cflags, new_nregs *
sizeof(
int));
365 re_list->
cflags = new_cflags;
369 new_regs = (regex_t **)realloc(
370 re_list->
regs, new_nregs *
sizeof(regex_t *));
376 re_list->
regs = new_regs;
380 for (pi = 0; pi < npatterns; pi++) {
382 preg = (regex_t *)calloc(1,
sizeof(regex_t));
387 if (!
re_compile(preg, patterns[pi], cflags)) {
409 "Could not compile list of regular expression patterns\n"
410 " -> memory allocation error\n");
470 if (mindex) *mindex = -1;
471 if (nsubs) *nsubs = 0;
472 if (pmatch) *pmatch = (regmatch_t *)NULL;
473 if (substrings) *substrings = (
char **)NULL;
477 __relist_free_results(re_list);
482 re_list->
string = strdup(
string);
491 for (ri = 0; ri < re_list->
nregs; ri++) {
493 if (re_list->
cflags[ri] & REG_NOSUB)
continue;
495 if (max_nsubs < re_list->regs[ri]->re_nsub) {
496 max_nsubs = re_list->
regs[ri]->re_nsub;
502 max_nmatch = max_nsubs + 1;
504 offsets = (regmatch_t *)malloc(max_nmatch *
sizeof(regmatch_t));
511 for (ri = 0; ri < re_list->
nregs; ri++) {
513 preg = re_list->
regs[ri];
514 status =
re_execute(preg,
string, max_nmatch, offsets, eflags);
516 if (status > 0)
break;
517 if (status == 0)
continue;
524 if (ri == re_list->
nregs) {
534 if (re_list->
cflags[ri] & REG_NOSUB) {
538 re_list->
nsubs = re_list->
regs[ri]->re_nsub;
553 if (mindex) *mindex = re_list->
mindex;
554 if (nsubs) *nsubs = re_list->
nsubs;
555 if (pmatch) *pmatch = re_list->
offsets;
556 if (substrings) *substrings = re_list->
substrs;
563 "Could not compare string to regular expressions list: '%s'\n"
564 " -> memory allocation error\n",
string);
580 __relist_free_results(re_list);
583 for (ri = 0; ri < re_list->
nregs; ri++) {
590 for (ri = 0; ri < re_list->
nregs; ri++) {
591 regfree(re_list->
regs[ri]);
592 free(re_list->
regs[ri]);