Changeset View
Changeset View
Standalone View
Standalone View
head/contrib/less/pattern.c
Show All 14 Lines | |||||
#include "pattern.h" | #include "pattern.h" | ||||
extern int caseless; | extern int caseless; | ||||
/* | /* | ||||
* Compile a search pattern, for future use by match_pattern. | * Compile a search pattern, for future use by match_pattern. | ||||
*/ | */ | ||||
static int | static int | ||||
compile_pattern2(pattern, search_type, comp_pattern, show_error) | compile_pattern2(char *pattern, int search_type, void **comp_pattern, int show_error) | ||||
char *pattern; | |||||
int search_type; | |||||
void **comp_pattern; | |||||
int show_error; | |||||
{ | { | ||||
if (search_type & SRCH_NO_REGEX) | if (search_type & SRCH_NO_REGEX) | ||||
return (0); | return (0); | ||||
{ | { | ||||
#if HAVE_GNU_REGEX | #if HAVE_GNU_REGEX | ||||
struct re_pattern_buffer *comp = (struct re_pattern_buffer *) | struct re_pattern_buffer *comp = (struct re_pattern_buffer *) | ||||
ecalloc(1, sizeof(struct re_pattern_buffer)); | ecalloc(1, sizeof(struct re_pattern_buffer)); | ||||
struct re_pattern_buffer **pcomp = | struct re_pattern_buffer **pcomp = | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | #endif | ||||
} | } | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Like compile_pattern2, but convert the pattern to lowercase if necessary. | * Like compile_pattern2, but convert the pattern to lowercase if necessary. | ||||
*/ | */ | ||||
public int | public int | ||||
compile_pattern(pattern, search_type, comp_pattern) | compile_pattern(char *pattern, int search_type, void **comp_pattern) | ||||
char *pattern; | |||||
int search_type; | |||||
void **comp_pattern; | |||||
{ | { | ||||
char *cvt_pattern; | char *cvt_pattern; | ||||
int result; | int result; | ||||
if (caseless != OPT_ONPLUS) | if (caseless != OPT_ONPLUS) | ||||
cvt_pattern = pattern; | cvt_pattern = pattern; | ||||
else | else | ||||
{ | { | ||||
cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC)); | cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC)); | ||||
cvt_text(cvt_pattern, pattern, (int *)NULL, (int *)NULL, CVT_TO_LC); | cvt_text(cvt_pattern, pattern, (int *)NULL, (int *)NULL, CVT_TO_LC); | ||||
} | } | ||||
result = compile_pattern2(cvt_pattern, search_type, comp_pattern, 1); | result = compile_pattern2(cvt_pattern, search_type, comp_pattern, 1); | ||||
if (cvt_pattern != pattern) | if (cvt_pattern != pattern) | ||||
free(cvt_pattern); | free(cvt_pattern); | ||||
return (result); | return (result); | ||||
} | } | ||||
/* | /* | ||||
* Forget that we have a compiled pattern. | * Forget that we have a compiled pattern. | ||||
*/ | */ | ||||
public void | public void | ||||
uncompile_pattern(pattern) | uncompile_pattern(void **pattern) | ||||
void **pattern; | |||||
{ | { | ||||
#if HAVE_GNU_REGEX | #if HAVE_GNU_REGEX | ||||
struct re_pattern_buffer **pcomp = (struct re_pattern_buffer **) pattern; | struct re_pattern_buffer **pcomp = (struct re_pattern_buffer **) pattern; | ||||
if (*pcomp != NULL) | if (*pcomp != NULL) | ||||
regfree(*pcomp); | regfree(*pcomp); | ||||
*pcomp = NULL; | *pcomp = NULL; | ||||
#endif | #endif | ||||
#if HAVE_POSIX_REGCOMP | #if HAVE_POSIX_REGCOMP | ||||
Show All 25 Lines | #if HAVE_V8_REGCOMP | ||||
*pcomp = NULL; | *pcomp = NULL; | ||||
#endif | #endif | ||||
} | } | ||||
/* | /* | ||||
* Can a pattern be successfully compiled? | * Can a pattern be successfully compiled? | ||||
*/ | */ | ||||
public int | public int | ||||
valid_pattern(pattern) | valid_pattern(char *pattern) | ||||
char *pattern; | |||||
{ | { | ||||
void *comp_pattern; | void *comp_pattern; | ||||
int result; | int result; | ||||
CLEAR_PATTERN(comp_pattern); | CLEAR_PATTERN(comp_pattern); | ||||
result = compile_pattern2(pattern, 0, &comp_pattern, 0); | result = compile_pattern2(pattern, 0, &comp_pattern, 0); | ||||
if (result != 0) | if (result != 0) | ||||
return (0); | return (0); | ||||
uncompile_pattern(&comp_pattern); | uncompile_pattern(&comp_pattern); | ||||
return (1); | return (1); | ||||
} | } | ||||
/* | /* | ||||
* Is a compiled pattern null? | * Is a compiled pattern null? | ||||
*/ | */ | ||||
public int | public int | ||||
is_null_pattern(pattern) | is_null_pattern(void *pattern) | ||||
void *pattern; | |||||
{ | { | ||||
#if HAVE_GNU_REGEX | #if HAVE_GNU_REGEX | ||||
return (pattern == NULL); | return (pattern == NULL); | ||||
#endif | #endif | ||||
#if HAVE_POSIX_REGCOMP | #if HAVE_POSIX_REGCOMP | ||||
return (pattern == NULL); | return (pattern == NULL); | ||||
#endif | #endif | ||||
#if HAVE_PCRE | #if HAVE_PCRE | ||||
Show All 13 Lines | |||||
#endif | #endif | ||||
} | } | ||||
/* | /* | ||||
* Simple pattern matching function. | * Simple pattern matching function. | ||||
* It supports no metacharacters like *, etc. | * It supports no metacharacters like *, etc. | ||||
*/ | */ | ||||
static int | static int | ||||
match(pattern, pattern_len, buf, buf_len, pfound, pend) | match(char *pattern, int pattern_len, char *buf, int buf_len, char **pfound, char **pend) | ||||
char *pattern; | |||||
int pattern_len; | |||||
char *buf; | |||||
int buf_len; | |||||
char **pfound, **pend; | |||||
{ | { | ||||
register char *pp, *lp; | char *pp, *lp; | ||||
register char *pattern_end = pattern + pattern_len; | char *pattern_end = pattern + pattern_len; | ||||
register char *buf_end = buf + buf_len; | char *buf_end = buf + buf_len; | ||||
for ( ; buf < buf_end; buf++) | for ( ; buf < buf_end; buf++) | ||||
{ | { | ||||
for (pp = pattern, lp = buf; ; pp++, lp++) | for (pp = pattern, lp = buf; ; pp++, lp++) | ||||
{ | { | ||||
char cp = *pp; | char cp = *pp; | ||||
char cl = *lp; | char cl = *lp; | ||||
if (caseless == OPT_ONPLUS && ASCII_IS_UPPER(cp)) | if (caseless == OPT_ONPLUS && ASCII_IS_UPPER(cp)) | ||||
Show All 15 Lines | match(char *pattern, int pattern_len, char *buf, int buf_len, char **pfound, char **pend) | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Perform a pattern match with the previously compiled pattern. | * Perform a pattern match with the previously compiled pattern. | ||||
* Set sp and ep to the start and end of the matched string. | * Set sp and ep to the start and end of the matched string. | ||||
*/ | */ | ||||
public int | public int | ||||
match_pattern(pattern, tpattern, line, line_len, sp, ep, notbol, search_type) | match_pattern(void *pattern, char *tpattern, char *line, int line_len, char **sp, char **ep, int notbol, int search_type) | ||||
void *pattern; | |||||
char *tpattern; | |||||
char *line; | |||||
int line_len; | |||||
char **sp; | |||||
char **ep; | |||||
int notbol; | |||||
int search_type; | |||||
{ | { | ||||
int matched; | int matched; | ||||
#if HAVE_GNU_REGEX | #if HAVE_GNU_REGEX | ||||
struct re_pattern_buffer *spattern = (struct re_pattern_buffer *) pattern; | struct re_pattern_buffer *spattern = (struct re_pattern_buffer *) pattern; | ||||
#endif | #endif | ||||
#if HAVE_POSIX_REGCOMP | #if HAVE_POSIX_REGCOMP | ||||
regex_t *spattern = (regex_t *) pattern; | regex_t *spattern = (regex_t *) pattern; | ||||
#endif | #endif | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |