[7d4fbcd] | 1 | #include <string.h> |
---|
| 2 | #include "owl.h" |
---|
| 3 | |
---|
[1aee7d9] | 4 | static const char fileIdent[] = "$Id$"; |
---|
| 5 | |
---|
[e187445] | 6 | void owl_regex_init(owl_regex *re) |
---|
| 7 | { |
---|
[7d4fbcd] | 8 | re->negate=0; |
---|
| 9 | re->string=NULL; |
---|
| 10 | } |
---|
| 11 | |
---|
[e187445] | 12 | int owl_regex_create(owl_regex *re, char *string) |
---|
| 13 | { |
---|
[7d4fbcd] | 14 | int ret; |
---|
| 15 | char buff1[LINE], buff2[LINE]; |
---|
| 16 | char *ptr; |
---|
| 17 | |
---|
| 18 | re->string=owl_strdup(string); |
---|
| 19 | |
---|
| 20 | ptr=string; |
---|
| 21 | re->negate=0; |
---|
| 22 | if (string[0]=='!') { |
---|
| 23 | ptr++; |
---|
| 24 | re->negate=1; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | /* set the regex */ |
---|
| 28 | ret=regcomp(&(re->re), ptr, REG_EXTENDED|REG_ICASE); |
---|
| 29 | if (ret) { |
---|
| 30 | regerror(ret, NULL, buff1, LINE); |
---|
| 31 | sprintf(buff2, "Error in regular expression: %s", buff1); |
---|
| 32 | owl_function_makemsg(buff2); |
---|
| 33 | owl_free(re->string); |
---|
[a6560fe] | 34 | re->string=NULL; |
---|
[7d4fbcd] | 35 | return(-1); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | return(0); |
---|
| 39 | } |
---|
| 40 | |
---|
[bc08664] | 41 | int owl_regex_create_quoted(owl_regex *re, char *string) |
---|
| 42 | { |
---|
| 43 | char *quoted; |
---|
| 44 | |
---|
| 45 | quoted=owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
---|
| 46 | owl_regex_create(re, quoted); |
---|
| 47 | owl_free(quoted); |
---|
| 48 | return(0); |
---|
| 49 | } |
---|
| 50 | |
---|
[e187445] | 51 | int owl_regex_compare(owl_regex *re, char *string) |
---|
| 52 | { |
---|
[7d4fbcd] | 53 | int out, ret; |
---|
| 54 | |
---|
| 55 | /* if the regex is not set we match */ |
---|
| 56 | if (!owl_regex_is_set(re)) { |
---|
| 57 | return(0); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | ret=regexec(&(re->re), string, 0, NULL, 0); |
---|
| 61 | out=ret; |
---|
| 62 | if (re->negate) { |
---|
| 63 | out=!out; |
---|
| 64 | } |
---|
| 65 | return(out); |
---|
| 66 | } |
---|
| 67 | |
---|
[e187445] | 68 | int owl_regex_is_set(owl_regex *re) |
---|
| 69 | { |
---|
[7d4fbcd] | 70 | if (re->string) return(1); |
---|
| 71 | return(0); |
---|
| 72 | } |
---|
| 73 | |
---|
[e187445] | 74 | char *owl_regex_get_string(owl_regex *re) |
---|
| 75 | { |
---|
[7d4fbcd] | 76 | return(re->string); |
---|
| 77 | } |
---|
| 78 | |
---|
[e187445] | 79 | void owl_regex_copy(owl_regex *a, owl_regex *b) |
---|
| 80 | { |
---|
[7d4fbcd] | 81 | b->negate=a->negate; |
---|
| 82 | b->string=owl_strdup(a->string); |
---|
| 83 | memcpy(&(b->re), &(a->re), sizeof(regex_t)); |
---|
| 84 | } |
---|
| 85 | |
---|
[e187445] | 86 | void owl_regex_free(owl_regex *re) |
---|
| 87 | { |
---|
[cb769bb] | 88 | if (re->string) { |
---|
| 89 | owl_free(re->string); |
---|
| 90 | regfree(&(re->re)); |
---|
| 91 | } |
---|
[7d4fbcd] | 92 | } |
---|