| 1 | #include <string.h> |
|---|
| 2 | #include "owl.h" |
|---|
| 3 | |
|---|
| 4 | void owl_regex_init(owl_regex *re) { |
|---|
| 5 | re->negate=0; |
|---|
| 6 | re->string=NULL; |
|---|
| 7 | } |
|---|
| 8 | |
|---|
| 9 | int owl_regex_create(owl_regex *re, char *string) { |
|---|
| 10 | int ret; |
|---|
| 11 | char buff1[LINE], buff2[LINE]; |
|---|
| 12 | char *ptr; |
|---|
| 13 | |
|---|
| 14 | re->string=owl_strdup(string); |
|---|
| 15 | |
|---|
| 16 | ptr=string; |
|---|
| 17 | re->negate=0; |
|---|
| 18 | if (string[0]=='!') { |
|---|
| 19 | ptr++; |
|---|
| 20 | re->negate=1; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | /* set the regex */ |
|---|
| 24 | ret=regcomp(&(re->re), ptr, REG_EXTENDED|REG_ICASE); |
|---|
| 25 | if (ret) { |
|---|
| 26 | regerror(ret, NULL, buff1, LINE); |
|---|
| 27 | sprintf(buff2, "Error in regular expression: %s", buff1); |
|---|
| 28 | owl_function_makemsg(buff2); |
|---|
| 29 | owl_free(re->string); |
|---|
| 30 | return(-1); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | return(0); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | int owl_regex_compare(owl_regex *re, char *string) { |
|---|
| 37 | int out, ret; |
|---|
| 38 | |
|---|
| 39 | /* if the regex is not set we match */ |
|---|
| 40 | if (!owl_regex_is_set(re)) { |
|---|
| 41 | return(0); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | ret=regexec(&(re->re), string, 0, NULL, 0); |
|---|
| 45 | out=ret; |
|---|
| 46 | if (re->negate) { |
|---|
| 47 | out=!out; |
|---|
| 48 | } |
|---|
| 49 | return(out); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | int owl_regex_is_set(owl_regex *re) { |
|---|
| 53 | if (re->string) return(1); |
|---|
| 54 | return(0); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | char *owl_regex_get_string(owl_regex *re) { |
|---|
| 58 | return(re->string); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void owl_regex_copy(owl_regex *a, owl_regex *b) { |
|---|
| 62 | b->negate=a->negate; |
|---|
| 63 | b->string=owl_strdup(a->string); |
|---|
| 64 | memcpy(&(b->re), &(a->re), sizeof(regex_t)); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void owl_regex_free(owl_regex *re) { |
|---|
| 68 | if (re->string) owl_free(re->string); |
|---|
| 69 | |
|---|
| 70 | /* do we need to free the regular expression? */ |
|---|
| 71 | |
|---|
| 72 | } |
|---|