1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/*
* $Id: pattern_list.c,v 1.7 2008/04/21 01:51:17 nrickerby Exp nrickerby $
*
* Author: Squirm derived http://www.senet.com.au/squirm/
* Project: Jesred http://ivs.cs.uni-magdeburg.de/~elkner/webtools/jesred/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* http://www.gnu.org/copyleft/gpl.html or ./gpl.html
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Thanks to Chris Foote, chris@senet.com.au - not much to change here ;-)
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#ifdef LINUX
#include <ctype.h> /* for tolower() */
#endif
#ifdef LOCAL_REGEX
#include "regex.h"
#else
#include<regex.h>
#endif
#include "main.h"
#include "log.h"
#include "util.h"
#include "pattern_list.h"
int count_parenthesis (char *pattern);
void add_to_plist(pattern_item, pattern_item **);
#ifdef USE_ACCEL
char * get_accel(char *, int *, int);
#endif
void
add_to_patterns(char *pattern, pattern_item **plist)
{
char * rgxurl = "";
char * repurl = "";
#ifdef USE_ACCEL
char * accel = "";
#endif
char * urlgroup = NULL;
regex_t compiled;
pattern_item rpattern;
int abort_type = 0;
int stored;
char type[BUFSIZE];
char arg1[BUFSIZE];
char arg2[BUFSIZE];
char arg3[BUFSIZE];
char arg4[BUFSIZE];
/* The regex_flags that we use are:
REG_EXTENDED
REG_NOSUB
REG_ICASE; */
int regex_flags = REG_EXTENDED;
rpattern.type = EXTENDED;
rpattern.case_sensitive = 1;
stored = sscanf(pattern, "%s %s %s %s %s", type, arg1, arg2, arg3, arg4);
if((stored < 2) || (stored > 4)) {
mylog(ERROR, "unable to get a pair of patterns in add_to_patterns() "
"for [%s]\n", pattern);
echo_mode = 1;
return;
}
if ( (strcmp(type, "regexug") == 0) || (strcmp(type, "regexiug") == 0) ) {
switch( stored )
{
case 3 : urlgroup = arg1;
rgxurl = arg2;
break;
case 4 : urlgroup = arg1;
rgxurl = arg2;
repurl = arg3;
break;
#ifdef USE_ACCEL
case 5 : urlgroup = arg1;
rgxurl = arg2;
repurl = arg3;
accel = arg4;
break;
#endif
default : mylog(ERROR, "unable to parse rule for [%s]\n", pattern);
echo_mode = 1;
break;
}
}
else if ( (strcmp(type, "regex") == 0) || (strcmp(type, "regexi") == 0) ) {
switch( stored )
{
case 2 : rgxurl = arg1;
strcpy(repurl, "");
break;
case 3 : rgxurl = arg1;
repurl = arg2;
break;
#ifdef USE_ACCEL
case 4 : rgxurl = arg1;
repurl = arg2;
accel = arg3;
break;
#endif
default : mylog(ERROR, "unable to parse rule for [%s]\n", pattern);
echo_mode = 1;
break;
}
}
else if (strcmp(type, "abortug") == 0) {
rpattern.type = ABORT;
abort_type = 1;
if ( stored == 3 ) {
urlgroup = arg1;
rgxurl = arg2;
} else {
mylog(ERROR, "unable to parse rule for [%s]\n", pattern);
echo_mode = 1;
}
}
else if (strcmp(type, "abort") == 0) {
rpattern.type = ABORT;
abort_type = 1;
if ( stored == 2 ) {
rgxurl = arg1;
} else {
mylog(ERROR, "unable to parse rule for [%s]\n", pattern);
echo_mode = 1;
}
}
else {
mylog(ERROR, "unable to parse rule for [%s]\n", pattern);
echo_mode = 1;
}
if((strcmp(type, "regexi") == 0) || (strcmp(type, "regexiug") == 0)) {
regex_flags |= REG_ICASE;
rpattern.case_sensitive = 0;
}
if(regcomp(&compiled, rgxurl, regex_flags)) {
mylog(ERROR, "Invalid regex [%s] in pattern file\n", rgxurl);
echo_mode = 1;
return;
}
rpattern.cpattern = compiled;
rpattern.pattern = (char *)malloc(sizeof(char) * (strlen(rgxurl) +1));
if(rpattern.pattern == NULL) {
mylog(ERROR, "unable to allocate memory in add_to_patterns()\n");
echo_mode = 1;
return;
}
strcpy(rpattern.pattern, rgxurl);
rpattern.replacement = (char *)malloc(sizeof(char) * (strlen(repurl) +1));
if(rpattern.replacement == NULL) {
mylog(ERROR, "unable to allocate memory in add_to_patterns()\n");
echo_mode = 1;
return;
}
strcpy(rpattern.replacement, repurl);
#ifdef USE_ACCEL
/* use accelerator string if it exists */
if(stored == 4) {
rpattern.has_accel = 1;
rpattern.accel = get_accel(accel, &rpattern.accel_type,
rpattern.case_sensitive);
if(rpattern.accel == NULL) {
mylog(ERROR, "unable to allocate memory from get_accel()\n");
echo_mode = 1;
return;
}
}
else {
rpattern.has_accel = 0;
rpattern.accel = NULL;
}
#endif
if ( urlgroup ) {
rpattern.urlgroup = (char *)malloc(sizeof(char) * (strlen(urlgroup) +1));
if(rpattern.urlgroup == NULL) {
mylog(ERROR, "unable to allocate memory in add_to_patterns()\n");
echo_mode = 1;
return;
}
strcpy(rpattern.urlgroup, urlgroup);
}
else {
rpattern.urlgroup = NULL;
}
add_to_plist(rpattern, plist);
}
#ifdef USE_ACCEL
char *
get_accel(char *accel, int *accel_type, int case_sensitive) {
/* returns the stripped accelerator string or NULL if memory can't be
allocated
converts the accel string to lower case if(case_sensitive) */
/* accel_type is assigned one of the values:
#define ACCEL_NORMAL 1
#define ACCEL_START 2
#define ACCEL_END 3 */
int len, i;
char *new_accel = NULL;
*accel_type = 0;
len = strlen(accel);
if(accel[0] == '^')
*accel_type = ACCEL_START;
if(accel[len - 1] == '$')
*accel_type = ACCEL_END;
if(! *accel_type)
*accel_type = ACCEL_NORMAL;
/* copy the strings */
new_accel = (char *)malloc(sizeof(char) * strlen(accel));
strcpy(new_accel,accel);
if(*accel_type == ACCEL_START || *accel_type == ACCEL_END) {
if(new_accel == NULL)
return NULL;
if(*accel_type == ACCEL_START) {
if(case_sensitive)
for(i = 0; i < len; i++)
new_accel[i] = accel[i+1];
else
for(i = 0; i < len; i++)
new_accel[i] = tolower(accel[i+1]);
}
if(*accel_type == ACCEL_END) {
if(case_sensitive)
for(i = 0; i < len - 1; i++)
new_accel[i] = accel[i];
else
for(i = 0; i < len - 1; i++)
new_accel[i] = tolower(accel[i]);
new_accel[i] = '\0';
}
}
else {
if(!case_sensitive) {
for(i = 0; i < len; i++)
new_accel[i] = tolower(accel[i]);
new_accel[i] = '\0';
}
}
return new_accel;
}
#endif
void
add_to_plist(pattern_item pattern, pattern_item **plist) {
pattern_item *curr;
pattern_item *new;
curr = NULL;
new = NULL;
if (! (*plist)) {
/* empty list */
*plist = xcalloc(1, sizeof(pattern_item));
new = *plist;
} else {
/* find end of list */
curr = *plist;
while(curr->next)
curr = curr->next;
new = xcalloc(1, sizeof(pattern_item));
curr->next = new;
}
if(! new) {
mylog(ERROR, "unable to allocate memory in add_to_plist()\n");
/* exit(3); */
echo_mode = 1;
return;
}
new->pattern = pattern.pattern;
new->replacement = pattern.replacement;
new->type = pattern.type;
#ifdef USE_ACCEL
new->has_accel = pattern.has_accel;
new->accel = pattern.accel;
new->accel_type = pattern.accel_type;
#endif
new->case_sensitive = pattern.case_sensitive;
new->urlgroup = pattern.urlgroup;
/* not sure whether we need to copy each item in the struct */
new->cpattern = pattern.cpattern;
new->next = NULL;
}
int count_parenthesis (char *pattern)
{
int lcount = 0;
int rcount = 0;
int i;
/* Traverse string looking for ( and ) */
for (i = 0; i < strlen (pattern); i++) {
/* We have found a left ( */
if (pattern [i] == '(') {
/* Do not count if there is a backslash */
if ((i != 0) && (pattern [i-1] == '\\'))
continue;
else
lcount++;
}
if (pattern [i] == ')') {
if ((i != 0) && (pattern [i-1] == '\\'))
continue;
else
rcount++;
}
}
/* Check that left == right */
if (lcount != rcount)
return (-1);
return (lcount);
}
#ifdef DEBUG
void
print_plist(pattern_item **plist) {
pattern_item *curr;
pattern_item *new;
curr = NULL;
new = NULL;
int count = 0;
if (! (*plist)) {
/* empty list */
printf("empty list\n");
} else {
/* find end of list */
curr = *plist;
while(curr) {
printf("rule %d\n", count);
switch( curr->type )
{
case 1 : printf( "\tnormal\n" ); break;
case 2 : printf( "\textended\n" ); break;
case 3 : printf( "\tabort\n" ); break;
}
printf("\tpattern \"%s\"\n", curr->pattern );
printf("\treplacement \"%s\"\n", curr->replacement );
printf("\turlgroup \"%s\"\n", curr->urlgroup );
curr = curr->next;
count++;
printf("\n");
}
}
}
#endif
void
plist_destroy(pattern_item **a)
{
pattern_item *b;
pattern_item *n;
for (b = *a; b; b = n) {
n = b->next;
safe_free(b->pattern);
safe_free(b->replacement);
#ifdef USE_ACCEL
if (b->accel)
safe_free(b->accel);
#endif
regfree(&(b->cpattern));
safe_free(b);
}
*a = NULL;
}
|