version 1.2, 2014/04/12 01:01:19 |
version 1.3, 2014/04/25 13:38:21 |
|
|
/* |
/* |
* Copyright (C) 1984-2011 Mark Nudelman |
* Copyright (C) 1984-2012 Mark Nudelman |
* |
* |
* You may distribute under the terms of either the GNU General Public |
* You may distribute under the terms of either the GNU General Public |
* License or the Less License, as specified in the README file. |
* License or the Less License, as specified in the README file. |
* |
* |
* For more information about less, or for information on how to |
* For more information, see the README file. |
* contact the author, see the README file. |
|
*/ |
*/ |
|
|
/* |
/* |
|
|
int search_type; |
int search_type; |
void **comp_pattern; |
void **comp_pattern; |
{ |
{ |
if ((search_type & SRCH_NO_REGEX) == 0) |
if (search_type & SRCH_NO_REGEX) |
|
return (0); |
|
{ |
|
#if HAVE_GNU_REGEX |
|
struct re_pattern_buffer *comp = (struct re_pattern_buffer *) |
|
ecalloc(1, sizeof(struct re_pattern_buffer)); |
|
struct re_pattern_buffer **pcomp = |
|
(struct re_pattern_buffer **) comp_pattern; |
|
re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); |
|
if (re_compile_pattern(pattern, strlen(pattern), comp)) |
{ |
{ |
|
free(comp); |
|
error("Invalid pattern", NULL_PARG); |
|
return (-1); |
|
} |
|
if (*pcomp != NULL) |
|
regfree(*pcomp); |
|
*pcomp = comp; |
|
#endif |
#if HAVE_POSIX_REGCOMP |
#if HAVE_POSIX_REGCOMP |
regex_t *comp = (regex_t *) ecalloc(1, sizeof(regex_t)); |
regex_t *comp = (regex_t *) ecalloc(1, sizeof(regex_t)); |
regex_t **pcomp = (regex_t **) comp_pattern; |
regex_t **pcomp = (regex_t **) comp_pattern; |
if (regcomp(comp, pattern, less_is_more ? 0 : REGCOMP_FLAG)) |
if (regcomp(comp, pattern, less_is_more ? 0 : REGCOMP_FLAG)) |
{ |
{ |
free(comp); |
free(comp); |
error("Invalid pattern", NULL_PARG); |
error("Invalid pattern", NULL_PARG); |
return (-1); |
return (-1); |
} |
} |
if (*pcomp != NULL) |
if (*pcomp != NULL) |
regfree(*pcomp); |
regfree(*pcomp); |
*pcomp = comp; |
*pcomp = comp; |
#endif |
#endif |
#if HAVE_PCRE |
#if HAVE_PCRE |
pcre *comp; |
pcre *comp; |
pcre **pcomp = (pcre **) comp_pattern; |
pcre **pcomp = (pcre **) comp_pattern; |
const char *errstring; |
constant char *errstring; |
int erroffset; |
int erroffset; |
PARG parg; |
PARG parg; |
comp = pcre_compile(pattern, 0, |
comp = pcre_compile(pattern, 0, |
&errstring, &erroffset, NULL); |
&errstring, &erroffset, NULL); |
if (comp == NULL) |
if (comp == NULL) |
{ |
{ |
parg.p_string = (char *) errstring; |
parg.p_string = (char *) errstring; |
error("%s", &parg); |
error("%s", &parg); |
return (-1); |
return (-1); |
} |
} |
*pcomp = comp; |
*pcomp = comp; |
#endif |
#endif |
#if HAVE_RE_COMP |
#if HAVE_RE_COMP |
PARG parg; |
PARG parg; |
int *pcomp = (int *) comp_pattern; |
int *pcomp = (int *) comp_pattern; |
if ((parg.p_string = re_comp(pattern)) != NULL) |
if ((parg.p_string = re_comp(pattern)) != NULL) |
{ |
{ |
error("%s", &parg); |
error("%s", &parg); |
return (-1); |
return (-1); |
} |
} |
*pcomp = 1; |
*pcomp = 1; |
#endif |
#endif |
#if HAVE_REGCMP |
#if HAVE_REGCMP |
char *comp; |
char *comp; |
char **pcomp = (char **) comp_pattern; |
char **pcomp = (char **) comp_pattern; |
if ((comp = regcmp(pattern, 0)) == NULL) |
if ((comp = regcmp(pattern, 0)) == NULL) |
{ |
{ |
error("Invalid pattern", NULL_PARG); |
error("Invalid pattern", NULL_PARG); |
return (-1); |
return (-1); |
} |
} |
if (pcomp != NULL) |
if (pcomp != NULL) |
free(*pcomp); |
free(*pcomp); |
*pcomp = comp; |
*pcomp = comp; |
#endif |
#endif |
#if HAVE_V8_REGCOMP |
#if HAVE_V8_REGCOMP |
struct regexp *comp; |
struct regexp *comp; |
struct regexp **pcomp = (struct regexp **) comp_pattern; |
struct regexp **pcomp = (struct regexp **) comp_pattern; |
if ((comp = regcomp(pattern)) == NULL) |
if ((comp = regcomp(pattern)) == NULL) |
{ |
{ |
/* |
/* |
* regcomp has already printed an error message |
* regcomp has already printed an error message |
* via regerror(). |
* via regerror(). |
*/ |
*/ |
return (-1); |
return (-1); |
} |
|
if (*pcomp != NULL) |
|
free(*pcomp); |
|
*pcomp = comp; |
|
#endif |
|
} |
} |
|
if (*pcomp != NULL) |
|
free(*pcomp); |
|
*pcomp = comp; |
|
#endif |
|
} |
return (0); |
return (0); |
} |
} |
|
|
|
|
uncompile_pattern(pattern) |
uncompile_pattern(pattern) |
void **pattern; |
void **pattern; |
{ |
{ |
|
#if HAVE_GNU_REGEX |
|
struct re_pattern_buffer **pcomp = (struct re_pattern_buffer **) pattern; |
|
if (*pcomp != NULL) |
|
regfree(*pcomp); |
|
*pcomp = NULL; |
|
#endif |
#if HAVE_POSIX_REGCOMP |
#if HAVE_POSIX_REGCOMP |
regex_t **pcomp = (regex_t **) pattern; |
regex_t **pcomp = (regex_t **) pattern; |
if (*pcomp != NULL) |
if (*pcomp != NULL) |
|
|
is_null_pattern(pattern) |
is_null_pattern(pattern) |
void *pattern; |
void *pattern; |
{ |
{ |
|
#if HAVE_GNU_REGEX |
|
return (pattern == NULL); |
|
#endif |
#if HAVE_POSIX_REGCOMP |
#if HAVE_POSIX_REGCOMP |
return (pattern == NULL); |
return (pattern == NULL); |
#endif |
#endif |
|
|
#if HAVE_V8_REGCOMP |
#if HAVE_V8_REGCOMP |
return (pattern == NULL); |
return (pattern == NULL); |
#endif |
#endif |
#if NO_REGEX |
|
return (search_pattern != NULL); |
|
#endif |
|
} |
} |
|
|
/* |
/* |
|
|
int search_type; |
int search_type; |
{ |
{ |
int matched; |
int matched; |
|
#if HAVE_GNU_REGEX |
|
struct re_pattern_buffer *spattern = (struct re_pattern_buffer *) pattern; |
|
#endif |
#if HAVE_POSIX_REGCOMP |
#if HAVE_POSIX_REGCOMP |
regex_t *spattern = (regex_t *) pattern; |
regex_t *spattern = (regex_t *) pattern; |
#endif |
#endif |
|
|
struct regexp *spattern = (struct regexp *) pattern; |
struct regexp *spattern = (struct regexp *) pattern; |
#endif |
#endif |
|
|
|
#if NO_REGEX |
|
search_type |= SRCH_NO_REGEX; |
|
#endif |
if (search_type & SRCH_NO_REGEX) |
if (search_type & SRCH_NO_REGEX) |
matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); |
matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); |
else |
else |
{ |
{ |
|
#if HAVE_GNU_REGEX |
|
{ |
|
struct re_registers search_regs; |
|
regoff_t *starts = (regoff_t *) ecalloc(1, sizeof (regoff_t)); |
|
regoff_t *ends = (regoff_t *) ecalloc(1, sizeof (regoff_t)); |
|
spattern->not_bol = notbol; |
|
re_set_registers(spattern, &search_regs, 1, starts, ends); |
|
matched = re_search(spattern, line, line_len, 0, line_len, &search_regs) >= 0; |
|
if (matched) |
|
{ |
|
*sp = line + search_regs.start[0]; |
|
*ep = line + search_regs.end[0]; |
|
} |
|
free(starts); |
|
free(ends); |
|
} |
|
#endif |
#if HAVE_POSIX_REGCOMP |
#if HAVE_POSIX_REGCOMP |
{ |
{ |
regmatch_t rm; |
regmatch_t rm; |
|
|
*sp = spattern->startp[0]; |
*sp = spattern->startp[0]; |
*ep = spattern->endp[0]; |
*ep = spattern->endp[0]; |
} |
} |
#endif |
|
#if NO_REGEX |
|
matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); |
|
#endif |
#endif |
} |
} |
matched = (!(search_type & SRCH_NO_MATCH) && matched) || |
matched = (!(search_type & SRCH_NO_MATCH) && matched) || |