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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
#include "PatternMatch.h"
typedef enum {MATCH_FAIL, MATCH_FOUND, MATCH_ONE, MATCH_MANY, MATCH_REPEAT, MATCH_CONT, MATCH_QUOT, MATCH_START, MATCH_CHARLIST, MATCH_HEX, MATCH_OCTAL, MATCH_SWITCH_ON, MATCH_SWITCH_OFF} TPMatchElements;
//Gets Called recursively
int pmatch_char(char **P_PtrPtr, char **S_PtrPtr, int *Flags);
int pmatch_search(char *Pattern, char **S_PtrPtr, char *S_End, char **Start, char **End, int *Flags);
int pmatch_ascii(char *P_Ptr,char S_Char,int Type)
{
char ValStr[4];
int P_Char=-1;
if (Type==MATCH_HEX)
{
strncpy(ValStr,P_Ptr,2);
P_Char=strtol(P_Ptr,NULL,16);
}
if (Type==MATCH_OCTAL)
{
strncpy(ValStr,P_Ptr,3);
P_Char=strtol(P_Ptr,NULL,8);
}
if (P_Char==-1) return(FALSE);
if (P_Char==S_Char) return(TRUE);
return(FALSE);
}
void pmatch_switch(char SwitchType, char SwitchOnOrOff, int *Flags)
{
int NewFlag=0, OnOrOff=FALSE;
if (SwitchOnOrOff==MATCH_SWITCH_ON) OnOrOff=TRUE;
else if (SwitchOnOrOff==MATCH_SWITCH_OFF) OnOrOff=FALSE;
else return;
switch (SwitchType)
{
//These switches have the opposite meaning to the flags they control.
//+C turns case sensitivity on, but the flag is 'PMATCH_NOCASE' that
//turns it off, so we need to invert the sense of 'OnOrOff'
case 'C': NewFlag=PMATCH_NOCASE; OnOrOff= !OnOrOff; break;
case 'W': NewFlag=PMATCH_NOWILDCARDS; OnOrOff= !OnOrOff; break;
case 'X': NewFlag=PMATCH_NOEXTRACT; OnOrOff= !OnOrOff; break;
case 'N': NewFlag=PMATCH_NEWLINEEND; break;
}
if (OnOrOff) *Flags |= NewFlag;
else *Flags &= ~NewFlag;
}
int pmatch_quot(char **P_PtrPtr, char **S_PtrPtr, int *Flags)
{
int result=MATCH_FAIL, OldFlags;
char P_Char, S_Char, *OldPos;
P_Char=**P_PtrPtr;
S_Char=**S_PtrPtr;
switch (P_Char)
{
case 'b': if (S_Char=='\b') result=MATCH_ONE; break;
case 'e': if (S_Char==27) result=MATCH_ONE; break; //escape
case 'n': if (S_Char=='\n') result=MATCH_ONE; break;
case 'r': if (S_Char=='\r') result=MATCH_ONE; break;
case 't': if (S_Char==' ') result=MATCH_ONE; break;
case 'l': if (islower(S_Char)) result=MATCH_ONE; break;
case 'x': result=MATCH_HEX; break;
case 'A': if (isalpha(S_Char)) result=MATCH_ONE; break;
case 'B': if (isalnum(S_Char)) result=MATCH_ONE; break;
case 'D': if (isdigit(S_Char)) result=MATCH_ONE; break;
case 'S':
/*
if ((S_Char=='\x1b') && (*((*S_PtrPtr)+1)=='['))
{
//treat vt escape seqences as space
while ((S_Char !='m') && (S_Char != '\0'))
{
(*S_PtrPtr)++;
S_Char=**S_PtrPtr;
}
result=MATCH_ONE;
}
*/
if (isspace(S_Char)) result=MATCH_ONE;
break;
case 'P': if (ispunct(S_Char)) result=MATCH_ONE; break;
case 'X': if (isxdigit(S_Char)) result=MATCH_ONE; break;
case 'U': if (isupper(S_Char)) result=MATCH_ONE; break;
case '+': result=MATCH_SWITCH_ON; break;
case '-': result=MATCH_SWITCH_OFF; break;
default: if (S_Char==P_Char) result=MATCH_ONE; break;
}
switch (result)
{
case MATCH_ONE:
(*P_PtrPtr)++;
break;
case MATCH_HEX:
if (! pmatch_ascii((*P_PtrPtr)+1,S_Char,MATCH_HEX)) return(MATCH_FAIL);
(*P_PtrPtr)+=2;
break;
case MATCH_OCTAL:
if (! pmatch_ascii((*P_PtrPtr)+1,S_Char,MATCH_OCTAL)) return(MATCH_FAIL);
(*P_PtrPtr)+=3;
break;
case MATCH_SWITCH_ON:
case MATCH_SWITCH_OFF:
//some switches need to be applied in order for a pattern to match
//(like the case-insensitive switch) others should only be applied if
//it matches. So we apply the switch, but if the subsequent pmatch_char fails
//we unapply it
OldFlags=*Flags;
OldPos=*P_PtrPtr;
(*P_PtrPtr)++; //go past the + or - to the actual type
pmatch_switch(**P_PtrPtr, result, Flags);
(*P_PtrPtr)++;
result=pmatch_char(P_PtrPtr, S_PtrPtr, Flags);
if ((result==MATCH_FAIL) || (result==MATCH_CONT))
{
*P_PtrPtr=OldPos;
*Flags=OldFlags;
}
return(result);
break;
case MATCH_FAIL:
if (*Flags & PMATCH_SUBSTR) return(MATCH_CONT);
return(MATCH_FAIL);
break;
}
return(MATCH_ONE);
}
#define CHARLIST_NOT 1
int pmatch_charlist(char **P_PtrPtr,char S_Char, int Flags)
{
char P_Char, Prev_Char=0;
int result=MATCH_CONT;
int mode=0;
while (**P_PtrPtr != '\0')
{
if (Flags & PMATCH_NOCASE) P_Char=tolower(**P_PtrPtr);
else P_Char=**P_PtrPtr;
if (P_Char==']') break;
switch (P_Char)
{
case '\\':
(*P_PtrPtr)++;
if (Flags & PMATCH_NOCASE) P_Char=tolower(**P_PtrPtr);
else P_Char=**P_PtrPtr;
break;
case '-':
(*P_PtrPtr)++;
if (Flags & PMATCH_NOCASE) P_Char=tolower(**P_PtrPtr);
else P_Char=**P_PtrPtr;
if ((S_Char >= Prev_Char) && (S_Char <= P_Char)) result=MATCH_ONE;
break;
case '!':
mode |= CHARLIST_NOT;
break;
default:
if (P_Char == S_Char) result=MATCH_ONE;
break;
}
Prev_Char=P_Char;
(*P_PtrPtr)++;
}
//go beyond ']'
(*P_PtrPtr)++;
if (mode & CHARLIST_NOT)
{
if (result==MATCH_ONE) result=MATCH_CONT;
else result=MATCH_ONE;
}
return(result);
}
int pmatch_repeat(char **P_PtrPtr, char **S_PtrPtr, char *S_End, int *Flags)
{
char *SubPattern=NULL, *sp_ptr, *ptr;
int count=0, i, val=0, result=MATCH_FAIL;
ptr=*P_PtrPtr;
while ( ((**P_PtrPtr) != '}') && ((**P_PtrPtr) != '\0') ) (*P_PtrPtr)++;
if ((**P_PtrPtr)=='\0') return(MATCH_FAIL);
SubPattern=CopyStrLen(SubPattern,ptr,*P_PtrPtr - ptr);
sp_ptr=strchr(SubPattern,'|');
if (sp_ptr)
{
*sp_ptr='\0';
sp_ptr++;
count=atoi(SubPattern);
for (i=0; i < count; i++)
{
val=0;
result=pmatch_search(sp_ptr, S_PtrPtr, S_End, NULL, NULL, &val);
if (result==MATCH_CONT) result=MATCH_FAIL;
if (result==MATCH_FAIL) break;
}
}
(*S_PtrPtr)--;
(*P_PtrPtr)++;
DestroyString(SubPattern);
return(result);
}
int pmatch_char(char **P_PtrPtr, char **S_PtrPtr, int *Flags)
{
char P_Char, S_Char, *P_Start;
int result=MATCH_FAIL;
P_Start=*P_PtrPtr;
if (*Flags & PMATCH_NOCASE)
{
P_Char=tolower(**P_PtrPtr);
S_Char=tolower(**S_PtrPtr);
}
else
{
P_Char=**P_PtrPtr;
S_Char=**S_PtrPtr;
}
//we must still honor switches even if 'nowildcards' is set, as we may want to turn
//'nowildcards' off, or turn case or extraction features on or off
if (*Flags & PMATCH_NOWILDCARDS)
{
if (
(P_Char=='\\') &&
((*(*P_PtrPtr+1)=='+') || (*(*P_PtrPtr+1)=='-'))
) /*This is a switch, fall through and process it */ ;
else if (P_Char==S_Char) return(MATCH_ONE);
else return(MATCH_FAIL);
}
switch (P_Char)
{
case '\0': result=MATCH_FOUND; break;
case '|': result=MATCH_FOUND; break;
case '*': result=MATCH_MANY;break;
case '?': (*P_PtrPtr)++; result=MATCH_ONE;break;
case '^': (*P_PtrPtr)++; result=MATCH_START; break;
case '$':
if (S_Char=='\0') result=MATCH_FOUND;
else if ((*Flags & PMATCH_NEWLINEEND) && (S_Char=='\n')) result=MATCH_FOUND;
break;
//This is akin to $ (match end) but matches any 'breaking' character
case '%':
switch (S_Char)
{
case '\0':
case '\r':
case '\n':
case ' ':
case ' ':
case '-':
case '_':
case '.':
case ',':
case ':':
case ';':
result=MATCH_FOUND;
break;
}
break;
case '[': (*P_PtrPtr)++; result=pmatch_charlist(P_PtrPtr,S_Char,*Flags); break;
case '{': (*P_PtrPtr)++; result=MATCH_REPEAT; break;
case '\\':
//results here can either be MATCH_FAIL, MATCH_CONT, MATCH_ONE
(*P_PtrPtr)++;
result=pmatch_quot(P_PtrPtr, S_PtrPtr, Flags);
break;
default:
(*P_PtrPtr)++;
if (P_Char==S_Char) result=MATCH_ONE;
else if (*Flags & PMATCH_SUBSTR) result=MATCH_CONT;
else result=MATCH_FAIL;
break;
}
if ((result==MATCH_CONT) || (result==MATCH_FAIL)) *(P_PtrPtr)=P_Start;
return(result);
}
//Somewhat ugly, as we need to iterate through the string, so we need it passed as a **
int pmatch_search(char *Pattern, char **S_PtrPtr, char *S_End, char **MatchStart, char **MatchEnd, int *Flags)
{
int result;
char *P_Ptr, *ptr, *S_Start;
if (MatchStart) *MatchStart=NULL;
if (MatchEnd) *MatchEnd=NULL;
P_Ptr=Pattern;
S_Start=*S_PtrPtr;
result=pmatch_char(&P_Ptr, S_PtrPtr, Flags);
while (*S_PtrPtr <= S_End)
{
switch (result)
{
case MATCH_FAIL:
if (*Flags & PMATCH_SUBSTR) return(MATCH_CONT);
return(MATCH_FAIL);
break;
//Match failed, but we're looking for a substring of 'String' so continue searching for match
case MATCH_CONT:
if (! (*Flags & PMATCH_SUBSTR)) return(MATCH_FAIL);
//if we were some ways through a pattern before hitting the character
//that failed the match, then we must rewind to reconsider that character
//as a fresh match
//if ((*P_Ptr != *Pattern) && (*Pattern != '^')) (*S_PtrPtr)--;
if ((*P_Ptr != *Pattern) ) (*S_PtrPtr)--;
P_Ptr=Pattern;
break;
case MATCH_START:
if (*Flags & PMATCH_NOTSTART) return(MATCH_FAIL);
(*S_PtrPtr)--; //naughty, were are now pointing before String, but the
//S_Ptr++ below will correct this
break;
case MATCH_FOUND:
if (*Flags & PMATCH_NOEXTRACT)
{
//This is to prevent returning NULL strings in 'MatchStart' and 'MatchEnd'
if (MatchStart && (! *MatchStart)) *MatchStart=S_Start;
if (MatchEnd && (! *MatchEnd)) *MatchEnd=*MatchStart;
}
//else if (MatchEnd) *MatchEnd=*S_PtrPtr;
return(MATCH_ONE);
break;
//Match many is a special case. We have too look ahead to see if the next char
//Matches, and thus takes us out of 'match many' howerver, If the call to pmatch_char
//fails then we have to rewind the pattern pointer to match many's '*' and go round again
case MATCH_MANY:
ptr=P_Ptr;
P_Ptr++;
//pmatch_char will think that if we've reached the end of the pattern, then we've got
//a match, regardless of where we are in the string. However, this is the one case where
//'*' needs to be a little 'greedy'. If the last item in the pattern was '*' (match many)
//then we need to continue until we read the end of the string
if (*P_Ptr=='\0')
{
if (*S_PtrPtr == S_End) result=MATCH_FOUND;
else result=MATCH_FAIL;
}
else
{
if (*S_PtrPtr == S_End) result=MATCH_FAIL;
else result=pmatch_char(&P_Ptr, S_PtrPtr, Flags);
}
if ((result==MATCH_FAIL) || (result==MATCH_CONT)) P_Ptr=ptr;
break;
case MATCH_REPEAT:
result=pmatch_repeat(&P_Ptr, S_PtrPtr, S_End, Flags);
if (result==MATCH_FAIL)
{
if (*Flags & PMATCH_SUBSTR) return(MATCH_CONT);
return(MATCH_FAIL);
}
break;
}
if ((result==MATCH_CONT) )
{
if (MatchStart) *MatchStart=NULL;
if (MatchEnd) *MatchEnd=NULL;
}
else if (! (*Flags & PMATCH_NOEXTRACT))
{
if (MatchStart && (*S_PtrPtr >= S_Start) && (! *MatchStart)) *MatchStart=*S_PtrPtr;
if (MatchEnd && ((*(S_PtrPtr)+1) < S_End)) *MatchEnd=(*S_PtrPtr)+1;
}
//Handle 'MATCH_FOUND' in the switch statement, don't iterate further through Pattern or String
if (result==MATCH_FOUND) continue;
(*S_PtrPtr)++;
//if (*S_PtrPtr > S_End) break;
result=pmatch_char(&P_Ptr, S_PtrPtr, Flags);
}
//if pattern not exhausted then we didn't get a match
if (*P_Ptr !='\0') return(MATCH_CONT);
return(MATCH_ONE);
}
//Wrapper around pmatch_search to make it more user friendly
int pmatch_one(char *Pattern, char *String, int len, char **Start, char **End, int Flags)
{
char *S_Ptr, *S_End;
S_Ptr=String;
S_End=String+len;
if (Start) *Start=NULL;
if (End) *End=NULL;
return(pmatch_search(Pattern, &S_Ptr, S_End, Start, End, &Flags));
}
int pmatch_process(char **Compiled, char *String, int len, ListNode *Matches, int iFlags)
{
//p_ptr points to the pattern from 'Compiled' that's currently being
//tested. s_ptr holds our progress through the string
char **p_ptr;
char *s_ptr, *s_end;
char *Start=NULL, *End=NULL;
int result, Flags;
TPMatch *Match;
int NoOfItems=0;
Flags=iFlags &= ~PMATCH_SUBSTR;
s_end=String+len;
//We handle PMATCH substr in this function
for (p_ptr=Compiled; *p_ptr != NULL; p_ptr++)
{
for (s_ptr=String; s_ptr < s_end; s_ptr++)
{
result=pmatch_one(*p_ptr, s_ptr, s_end-s_ptr, &Start, &End, Flags);
if (result==MATCH_ONE)
{
NoOfItems++;
if (Matches)
{
Match=(TPMatch *) calloc(1, sizeof(TPMatch));
Match->Start=Start;
Match->End=End;
ListAddItem(Matches,Match);
}
}
else if (result==MATCH_FAIL) break;
}
Flags |= PMATCH_NOTSTART;
}
return(NoOfItems);
}
void pmatch_compile(char *Pattern, char ***Compiled)
{
int NoOfRecords=0, NoOfItems=0;
char *ptr;
ptr=Pattern;
while (ptr && (*ptr != '\0'))
{
//while ((*ptr=='?') || (*ptr=='*')) ptr++;
NoOfItems++;
if (NoOfItems >= NoOfRecords)
{
NoOfRecords+=10;
*Compiled=(char **) realloc(*Compiled,NoOfRecords*sizeof(char *));
}
(*Compiled)[NoOfItems-1]=ptr;
while ((*ptr !='\0') && (*ptr != '|')) ptr++;
if (*ptr=='|') ptr++;
}
*Compiled=(char **) realloc(*Compiled,(NoOfRecords+1)*sizeof(char *));
(*Compiled)[NoOfItems]=NULL;
}
int pmatch(char *Pattern, char *String, int Len, ListNode *Matches, int Flags)
{
char *ptr, **Compiled=NULL;
int result, len;
pmatch_compile(Pattern,&Compiled);
ptr=String;
len=Len;
result=pmatch_process(Compiled, ptr, len, Matches, Flags);
if (Compiled) free(Compiled);
return(result);
}
|