[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[1aee7d9] | 3 | static const char fileIdent[] = "$Id$"; |
---|
| 4 | |
---|
[7d4fbcd] | 5 | #define OWL_FILTERELEMENT_NULL 0 |
---|
| 6 | #define OWL_FILTERELEMENT_TRUE 1 |
---|
| 7 | #define OWL_FILTERELEMENT_FALSE 2 |
---|
| 8 | #define OWL_FILTERELEMENT_OPENBRACE 3 |
---|
| 9 | #define OWL_FILTERELEMENT_CLOSEBRACE 4 |
---|
| 10 | #define OWL_FILTERELEMENT_AND 5 |
---|
| 11 | #define OWL_FILTERELEMENT_OR 6 |
---|
| 12 | #define OWL_FILTERELEMENT_NOT 7 |
---|
| 13 | #define OWL_FILTERELEMENT_RE 8 |
---|
[40458b9] | 14 | #define OWL_FILTERELEMENT_FILTER 9 |
---|
[32eed98] | 15 | #define OWL_FILTERELEMENT_PERL 10 |
---|
[7d4fbcd] | 16 | |
---|
[e187445] | 17 | void owl_filterelement_create_null(owl_filterelement *fe) |
---|
| 18 | { |
---|
[7d4fbcd] | 19 | fe->type=OWL_FILTERELEMENT_NULL; |
---|
| 20 | fe->field=NULL; |
---|
[40458b9] | 21 | fe->filtername=NULL; |
---|
[7d4fbcd] | 22 | } |
---|
| 23 | |
---|
[e187445] | 24 | void owl_filterelement_create_openbrace(owl_filterelement *fe) |
---|
| 25 | { |
---|
[40458b9] | 26 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 27 | fe->type=OWL_FILTERELEMENT_OPENBRACE; |
---|
| 28 | } |
---|
| 29 | |
---|
[e187445] | 30 | void owl_filterelement_create_closebrace(owl_filterelement *fe) |
---|
| 31 | { |
---|
[40458b9] | 32 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 33 | fe->type=OWL_FILTERELEMENT_CLOSEBRACE; |
---|
| 34 | } |
---|
| 35 | |
---|
[e187445] | 36 | void owl_filterelement_create_and(owl_filterelement *fe) |
---|
| 37 | { |
---|
[40458b9] | 38 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 39 | fe->type=OWL_FILTERELEMENT_AND; |
---|
| 40 | } |
---|
| 41 | |
---|
[e187445] | 42 | void owl_filterelement_create_or(owl_filterelement *fe) |
---|
| 43 | { |
---|
[40458b9] | 44 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 45 | fe->type=OWL_FILTERELEMENT_OR; |
---|
| 46 | } |
---|
| 47 | |
---|
[e187445] | 48 | void owl_filterelement_create_not(owl_filterelement *fe) |
---|
| 49 | { |
---|
[40458b9] | 50 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 51 | fe->type=OWL_FILTERELEMENT_NOT; |
---|
| 52 | } |
---|
| 53 | |
---|
[e187445] | 54 | void owl_filterelement_create_true(owl_filterelement *fe) |
---|
| 55 | { |
---|
[40458b9] | 56 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 57 | fe->type=OWL_FILTERELEMENT_TRUE; |
---|
| 58 | } |
---|
| 59 | |
---|
[e187445] | 60 | void owl_filterelement_create_false(owl_filterelement *fe) |
---|
| 61 | { |
---|
[40458b9] | 62 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 63 | fe->type=OWL_FILTERELEMENT_FALSE; |
---|
| 64 | } |
---|
| 65 | |
---|
[e187445] | 66 | void owl_filterelement_create_re(owl_filterelement *fe, char *field, char *re) |
---|
| 67 | { |
---|
[40458b9] | 68 | owl_filterelement_create_null(fe); |
---|
[7d4fbcd] | 69 | fe->type=OWL_FILTERELEMENT_RE; |
---|
| 70 | fe->field=owl_strdup(field); |
---|
| 71 | owl_regex_create(&(fe->re), re); |
---|
| 72 | } |
---|
| 73 | |
---|
[40458b9] | 74 | void owl_filterelement_create_filter(owl_filterelement *fe, char *name) |
---|
| 75 | { |
---|
| 76 | owl_filterelement_create_null(fe); |
---|
| 77 | fe->type=OWL_FILTERELEMENT_FILTER; |
---|
| 78 | fe->filtername=owl_strdup(name); |
---|
| 79 | } |
---|
| 80 | |
---|
[32eed98] | 81 | void owl_filterelement_create_perl(owl_filterelement *fe, char *name) |
---|
| 82 | { |
---|
| 83 | owl_filterelement_create_null(fe); |
---|
| 84 | fe->type=OWL_FILTERELEMENT_PERL; |
---|
| 85 | fe->filtername=owl_strdup(name); |
---|
| 86 | } |
---|
| 87 | |
---|
[e187445] | 88 | void owl_filterelement_free(owl_filterelement *fe) |
---|
| 89 | { |
---|
[7d4fbcd] | 90 | if (fe->field) owl_free(fe->field); |
---|
[40458b9] | 91 | if (fe->filtername) owl_free(fe->filtername); |
---|
[7d4fbcd] | 92 | } |
---|
| 93 | |
---|
[e187445] | 94 | int owl_filterelement_is_null(owl_filterelement *fe) |
---|
| 95 | { |
---|
[7d4fbcd] | 96 | if (fe->type==OWL_FILTERELEMENT_NULL) return(1); |
---|
| 97 | return(0); |
---|
| 98 | } |
---|
| 99 | |
---|
[e187445] | 100 | int owl_filterelement_is_openbrace(owl_filterelement *fe) |
---|
| 101 | { |
---|
[7d4fbcd] | 102 | if (fe->type==OWL_FILTERELEMENT_OPENBRACE) return(1); |
---|
| 103 | return(0); |
---|
| 104 | } |
---|
| 105 | |
---|
[e187445] | 106 | int owl_filterelement_is_closebrace(owl_filterelement *fe) |
---|
| 107 | { |
---|
[7d4fbcd] | 108 | if (fe->type==OWL_FILTERELEMENT_CLOSEBRACE) return(1); |
---|
| 109 | return(0); |
---|
| 110 | } |
---|
| 111 | |
---|
[e187445] | 112 | int owl_filterelement_is_and(owl_filterelement *fe) |
---|
| 113 | { |
---|
[7d4fbcd] | 114 | if (fe->type==OWL_FILTERELEMENT_AND) return(1); |
---|
| 115 | return(0); |
---|
| 116 | } |
---|
| 117 | |
---|
[e187445] | 118 | int owl_filterelement_is_or(owl_filterelement *fe) |
---|
| 119 | { |
---|
[7d4fbcd] | 120 | if (fe->type==OWL_FILTERELEMENT_OR) return(1); |
---|
| 121 | return(0); |
---|
| 122 | } |
---|
| 123 | |
---|
[e187445] | 124 | int owl_filterelement_is_not(owl_filterelement *fe) |
---|
| 125 | { |
---|
[7d4fbcd] | 126 | if (fe->type==OWL_FILTERELEMENT_NOT) return(1); |
---|
| 127 | return(0); |
---|
| 128 | } |
---|
| 129 | |
---|
[e187445] | 130 | int owl_filterelement_is_true(owl_filterelement *fe) |
---|
| 131 | { |
---|
[7d4fbcd] | 132 | if (fe->type==OWL_FILTERELEMENT_TRUE) return(1); |
---|
| 133 | return(0); |
---|
| 134 | } |
---|
| 135 | |
---|
[e187445] | 136 | int owl_filterelement_is_false(owl_filterelement *fe) |
---|
| 137 | { |
---|
[7d4fbcd] | 138 | if (fe->type==OWL_FILTERELEMENT_FALSE) return(1); |
---|
| 139 | return(0); |
---|
| 140 | } |
---|
| 141 | |
---|
[e187445] | 142 | int owl_filterelement_is_re(owl_filterelement *fe) |
---|
| 143 | { |
---|
[7d4fbcd] | 144 | if (fe->type==OWL_FILTERELEMENT_RE) return(1); |
---|
| 145 | return(0); |
---|
| 146 | } |
---|
| 147 | |
---|
[32eed98] | 148 | int owl_filterelement_is_perl(owl_filterelement *fe) |
---|
| 149 | { |
---|
| 150 | if (fe->type==OWL_FILTERELEMENT_PERL) return(1); |
---|
| 151 | return(0); |
---|
| 152 | } |
---|
| 153 | |
---|
[e187445] | 154 | owl_regex *owl_filterelement_get_re(owl_filterelement *fe) |
---|
| 155 | { |
---|
[7d4fbcd] | 156 | return(&(fe->re)); |
---|
| 157 | } |
---|
| 158 | |
---|
[40458b9] | 159 | int owl_filterelement_is_filter(owl_filterelement *fe) |
---|
| 160 | { |
---|
| 161 | if (fe->type==OWL_FILTERELEMENT_FILTER) return(1); |
---|
| 162 | return(0); |
---|
| 163 | } |
---|
| 164 | |
---|
[e187445] | 165 | char *owl_filterelement_get_field(owl_filterelement *fe) |
---|
| 166 | { |
---|
[40458b9] | 167 | if (fe->field) return(fe->field); |
---|
| 168 | return("unknown-field"); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | char *owl_filterelement_get_filtername(owl_filterelement *fe) |
---|
| 172 | { |
---|
| 173 | if (fe->filtername) return(fe->filtername); |
---|
| 174 | return("unknown-filter"); |
---|
[7d4fbcd] | 175 | } |
---|
| 176 | |
---|
[e187445] | 177 | int owl_filterelement_is_value(owl_filterelement *fe) |
---|
| 178 | { |
---|
[7d4fbcd] | 179 | if ( (fe->type==OWL_FILTERELEMENT_TRUE) || |
---|
| 180 | (fe->type==OWL_FILTERELEMENT_FALSE) || |
---|
[40458b9] | 181 | (fe->type==OWL_FILTERELEMENT_RE) || |
---|
[32eed98] | 182 | (fe->type==OWL_FILTERELEMENT_PERL) || |
---|
[40458b9] | 183 | (fe->type==OWL_FILTERELEMENT_FILTER)) { |
---|
[7d4fbcd] | 184 | return(1); |
---|
| 185 | } |
---|
| 186 | return(0); |
---|
| 187 | } |
---|
| 188 | |
---|
[40458b9] | 189 | /* caller must free the return */ |
---|
[e187445] | 190 | char *owl_filterelement_to_string(owl_filterelement *fe) |
---|
| 191 | { |
---|
[7d4fbcd] | 192 | if (owl_filterelement_is_openbrace(fe)) { |
---|
| 193 | return(owl_strdup("( ")); |
---|
| 194 | } else if (owl_filterelement_is_closebrace(fe)) { |
---|
| 195 | return(owl_strdup(") ")); |
---|
| 196 | } else if (owl_filterelement_is_and(fe)) { |
---|
| 197 | return(owl_strdup("and ")); |
---|
| 198 | } else if (owl_filterelement_is_or(fe)) { |
---|
| 199 | return(owl_strdup("or ")); |
---|
| 200 | } else if (owl_filterelement_is_not(fe)) { |
---|
| 201 | return(owl_strdup("not ")); |
---|
| 202 | } else if (owl_filterelement_is_true(fe)) { |
---|
| 203 | return(owl_strdup("true ")); |
---|
| 204 | } else if (owl_filterelement_is_false(fe)) { |
---|
| 205 | return(owl_strdup("false ")); |
---|
| 206 | } else if (owl_filterelement_is_re(fe)) { |
---|
[40458b9] | 207 | return(owl_sprintf("%s %s ", fe->field, owl_regex_get_string(&(fe->re)))); |
---|
| 208 | } else if (owl_filterelement_is_filter(fe)) { |
---|
| 209 | return(owl_sprintf("filter %s ", fe->filtername)); |
---|
[32eed98] | 210 | } else if (owl_filterelement_is_perl(fe)) { |
---|
| 211 | return(owl_sprintf("perl %s ", fe->filtername)); |
---|
[7d4fbcd] | 212 | } |
---|
[40458b9] | 213 | |
---|
| 214 | return(owl_strdup("?")); |
---|
[7d4fbcd] | 215 | } |
---|