pattern.c (221715) | pattern.c (237613) |
---|---|
1/* | 1/* |
2 * Copyright (C) 1984-2011 Mark Nudelman | 2 * Copyright (C) 1984-2012 Mark Nudelman |
3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * | 3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * |
7 * For more information about less, or for information on how to 8 * contact the author, see the README file. | 7 * For more information, see the README file. |
9 */ 10 11/* 12 * Routines to do pattern matching. 13 */ 14 15#include "less.h" 16#include "pattern.h" --- 4 unchanged lines hidden (view full) --- 21 * Compile a search pattern, for future use by match_pattern. 22 */ 23 static int 24compile_pattern2(pattern, search_type, comp_pattern) 25 char *pattern; 26 int search_type; 27 void **comp_pattern; 28{ | 8 */ 9 10/* 11 * Routines to do pattern matching. 12 */ 13 14#include "less.h" 15#include "pattern.h" --- 4 unchanged lines hidden (view full) --- 20 * Compile a search pattern, for future use by match_pattern. 21 */ 22 static int 23compile_pattern2(pattern, search_type, comp_pattern) 24 char *pattern; 25 int search_type; 26 void **comp_pattern; 27{ |
29 if ((search_type & SRCH_NO_REGEX) == 0) | 28 if (search_type & SRCH_NO_REGEX) 29 return (0); 30 { 31#if HAVE_GNU_REGEX 32 struct re_pattern_buffer *comp = (struct re_pattern_buffer *) 33 ecalloc(1, sizeof(struct re_pattern_buffer)); 34 struct re_pattern_buffer **pcomp = 35 (struct re_pattern_buffer **) comp_pattern; 36 re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); 37 if (re_compile_pattern(pattern, strlen(pattern), comp)) |
30 { | 38 { |
39 free(comp); 40 error("Invalid pattern", NULL_PARG); 41 return (-1); 42 } 43 if (*pcomp != NULL) 44 regfree(*pcomp); 45 *pcomp = comp; 46#endif |
|
31#if HAVE_POSIX_REGCOMP | 47#if HAVE_POSIX_REGCOMP |
32 regex_t *comp = (regex_t *) ecalloc(1, sizeof(regex_t)); 33 regex_t **pcomp = (regex_t **) comp_pattern; 34 if (regcomp(comp, pattern, REGCOMP_FLAG)) 35 { 36 free(comp); 37 error("Invalid pattern", NULL_PARG); 38 return (-1); 39 } 40 if (*pcomp != NULL) 41 regfree(*pcomp); 42 *pcomp = comp; | 48 regex_t *comp = (regex_t *) ecalloc(1, sizeof(regex_t)); 49 regex_t **pcomp = (regex_t **) comp_pattern; 50 if (regcomp(comp, pattern, REGCOMP_FLAG)) 51 { 52 free(comp); 53 error("Invalid pattern", NULL_PARG); 54 return (-1); 55 } 56 if (*pcomp != NULL) 57 regfree(*pcomp); 58 *pcomp = comp; |
43#endif 44#if HAVE_PCRE | 59#endif 60#if HAVE_PCRE |
45 pcre *comp; 46 pcre **pcomp = (pcre **) comp_pattern; 47 const char *errstring; 48 int erroffset; 49 PARG parg; 50 comp = pcre_compile(pattern, 0, 51 &errstring, &erroffset, NULL); 52 if (comp == NULL) 53 { 54 parg.p_string = (char *) errstring; 55 error("%s", &parg); 56 return (-1); 57 } 58 *pcomp = comp; | 61 pcre *comp; 62 pcre **pcomp = (pcre **) comp_pattern; 63 constant char *errstring; 64 int erroffset; 65 PARG parg; 66 comp = pcre_compile(pattern, 0, 67 &errstring, &erroffset, NULL); 68 if (comp == NULL) 69 { 70 parg.p_string = (char *) errstring; 71 error("%s", &parg); 72 return (-1); 73 } 74 *pcomp = comp; |
59#endif 60#if HAVE_RE_COMP | 75#endif 76#if HAVE_RE_COMP |
61 PARG parg; 62 int *pcomp = (int *) comp_pattern; 63 if ((parg.p_string = re_comp(pattern)) != NULL) 64 { 65 error("%s", &parg); 66 return (-1); 67 } 68 *pcomp = 1; | 77 PARG parg; 78 int *pcomp = (int *) comp_pattern; 79 if ((parg.p_string = re_comp(pattern)) != NULL) 80 { 81 error("%s", &parg); 82 return (-1); 83 } 84 *pcomp = 1; |
69#endif 70#if HAVE_REGCMP | 85#endif 86#if HAVE_REGCMP |
71 char *comp; 72 char **pcomp = (char **) comp_pattern; 73 if ((comp = regcmp(pattern, 0)) == NULL) 74 { 75 error("Invalid pattern", NULL_PARG); 76 return (-1); 77 } 78 if (pcomp != NULL) 79 free(*pcomp); 80 *pcomp = comp; | 87 char *comp; 88 char **pcomp = (char **) comp_pattern; 89 if ((comp = regcmp(pattern, 0)) == NULL) 90 { 91 error("Invalid pattern", NULL_PARG); 92 return (-1); 93 } 94 if (pcomp != NULL) 95 free(*pcomp); 96 *pcomp = comp; |
81#endif 82#if HAVE_V8_REGCOMP | 97#endif 98#if HAVE_V8_REGCOMP |
83 struct regexp *comp; 84 struct regexp **pcomp = (struct regexp **) comp_pattern; 85 if ((comp = regcomp(pattern)) == NULL) 86 { 87 /* 88 * regcomp has already printed an error message 89 * via regerror(). 90 */ 91 return (-1); 92 } 93 if (*pcomp != NULL) 94 free(*pcomp); 95 *pcomp = comp; 96#endif | 99 struct regexp *comp; 100 struct regexp **pcomp = (struct regexp **) comp_pattern; 101 if ((comp = regcomp(pattern)) == NULL) 102 { 103 /* 104 * regcomp has already printed an error message 105 * via regerror(). 106 */ 107 return (-1); |
97 } | 108 } |
109 if (*pcomp != NULL) 110 free(*pcomp); 111 *pcomp = comp; 112#endif 113 } |
|
98 return (0); 99} 100 101/* 102 * Like compile_pattern2, but convert the pattern to lowercase if necessary. 103 */ 104 public int 105compile_pattern(pattern, search_type, comp_pattern) --- 19 unchanged lines hidden (view full) --- 125 126/* 127 * Forget that we have a compiled pattern. 128 */ 129 public void 130uncompile_pattern(pattern) 131 void **pattern; 132{ | 114 return (0); 115} 116 117/* 118 * Like compile_pattern2, but convert the pattern to lowercase if necessary. 119 */ 120 public int 121compile_pattern(pattern, search_type, comp_pattern) --- 19 unchanged lines hidden (view full) --- 141 142/* 143 * Forget that we have a compiled pattern. 144 */ 145 public void 146uncompile_pattern(pattern) 147 void **pattern; 148{ |
149#if HAVE_GNU_REGEX 150 struct re_pattern_buffer **pcomp = (struct re_pattern_buffer **) pattern; 151 if (*pcomp != NULL) 152 regfree(*pcomp); 153 *pcomp = NULL; 154#endif |
|
133#if HAVE_POSIX_REGCOMP 134 regex_t **pcomp = (regex_t **) pattern; 135 if (*pcomp != NULL) 136 regfree(*pcomp); 137 *pcomp = NULL; 138#endif 139#if HAVE_PCRE 140 pcre **pcomp = (pcre **) pattern; --- 21 unchanged lines hidden (view full) --- 162 163/* 164 * Is a compiled pattern null? 165 */ 166 public int 167is_null_pattern(pattern) 168 void *pattern; 169{ | 155#if HAVE_POSIX_REGCOMP 156 regex_t **pcomp = (regex_t **) pattern; 157 if (*pcomp != NULL) 158 regfree(*pcomp); 159 *pcomp = NULL; 160#endif 161#if HAVE_PCRE 162 pcre **pcomp = (pcre **) pattern; --- 21 unchanged lines hidden (view full) --- 184 185/* 186 * Is a compiled pattern null? 187 */ 188 public int 189is_null_pattern(pattern) 190 void *pattern; 191{ |
192#if HAVE_GNU_REGEX 193 return (pattern == NULL); 194#endif |
|
170#if HAVE_POSIX_REGCOMP 171 return (pattern == NULL); 172#endif 173#if HAVE_PCRE 174 return (pattern == NULL); 175#endif 176#if HAVE_RE_COMP 177 return (pattern == 0); 178#endif 179#if HAVE_REGCMP 180 return (pattern == NULL); 181#endif 182#if HAVE_V8_REGCOMP 183 return (pattern == NULL); 184#endif | 195#if HAVE_POSIX_REGCOMP 196 return (pattern == NULL); 197#endif 198#if HAVE_PCRE 199 return (pattern == NULL); 200#endif 201#if HAVE_RE_COMP 202 return (pattern == 0); 203#endif 204#if HAVE_REGCMP 205 return (pattern == NULL); 206#endif 207#if HAVE_V8_REGCOMP 208 return (pattern == NULL); 209#endif |
185#if NO_REGEX 186 return (search_pattern != NULL); 187#endif | |
188} 189 190/* 191 * Simple pattern matching function. 192 * It supports no metacharacters like *, etc. 193 */ 194 static int 195match(pattern, pattern_len, buf, buf_len, pfound, pend) --- 35 unchanged lines hidden (view full) --- 231 char *line; 232 int line_len; 233 char **sp; 234 char **ep; 235 int notbol; 236 int search_type; 237{ 238 int matched; | 210} 211 212/* 213 * Simple pattern matching function. 214 * It supports no metacharacters like *, etc. 215 */ 216 static int 217match(pattern, pattern_len, buf, buf_len, pfound, pend) --- 35 unchanged lines hidden (view full) --- 253 char *line; 254 int line_len; 255 char **sp; 256 char **ep; 257 int notbol; 258 int search_type; 259{ 260 int matched; |
261#if HAVE_GNU_REGEX 262 struct re_pattern_buffer *spattern = (struct re_pattern_buffer *) pattern; 263#endif |
|
239#if HAVE_POSIX_REGCOMP 240 regex_t *spattern = (regex_t *) pattern; 241#endif 242#if HAVE_PCRE 243 pcre *spattern = (pcre *) pattern; 244#endif 245#if HAVE_RE_COMP 246 int spattern = (int) pattern; 247#endif 248#if HAVE_REGCMP 249 char *spattern = (char *) pattern; 250#endif 251#if HAVE_V8_REGCOMP 252 struct regexp *spattern = (struct regexp *) pattern; 253#endif 254 | 264#if HAVE_POSIX_REGCOMP 265 regex_t *spattern = (regex_t *) pattern; 266#endif 267#if HAVE_PCRE 268 pcre *spattern = (pcre *) pattern; 269#endif 270#if HAVE_RE_COMP 271 int spattern = (int) pattern; 272#endif 273#if HAVE_REGCMP 274 char *spattern = (char *) pattern; 275#endif 276#if HAVE_V8_REGCOMP 277 struct regexp *spattern = (struct regexp *) pattern; 278#endif 279 |
280#if NO_REGEX 281 search_type |= SRCH_NO_REGEX; 282#endif |
|
255 if (search_type & SRCH_NO_REGEX) 256 matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); 257 else 258 { | 283 if (search_type & SRCH_NO_REGEX) 284 matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); 285 else 286 { |
287#if HAVE_GNU_REGEX 288 { 289 struct re_registers search_regs; 290 regoff_t *starts = (regoff_t *) ecalloc(1, sizeof (regoff_t)); 291 regoff_t *ends = (regoff_t *) ecalloc(1, sizeof (regoff_t)); 292 spattern->not_bol = notbol; 293 re_set_registers(spattern, &search_regs, 1, starts, ends); 294 matched = re_search(spattern, line, line_len, 0, line_len, &search_regs) >= 0; 295 if (matched) 296 { 297 *sp = line + search_regs.start[0]; 298 *ep = line + search_regs.end[0]; 299 } 300 free(starts); 301 free(ends); 302 } 303#endif |
|
259#if HAVE_POSIX_REGCOMP 260 { 261 regmatch_t rm; 262 int flags = (notbol) ? REG_NOTBOL : 0; 263 matched = !regexec(spattern, line, 1, &rm, flags); 264 if (matched) 265 { 266#ifndef __WATCOMC__ --- 39 unchanged lines hidden (view full) --- 306 matched = regexec(spattern, line); 307#endif 308 if (matched) 309 { 310 *sp = spattern->startp[0]; 311 *ep = spattern->endp[0]; 312 } 313#endif | 304#if HAVE_POSIX_REGCOMP 305 { 306 regmatch_t rm; 307 int flags = (notbol) ? REG_NOTBOL : 0; 308 matched = !regexec(spattern, line, 1, &rm, flags); 309 if (matched) 310 { 311#ifndef __WATCOMC__ --- 39 unchanged lines hidden (view full) --- 351 matched = regexec(spattern, line); 352#endif 353 if (matched) 354 { 355 *sp = spattern->startp[0]; 356 *ep = spattern->endp[0]; 357 } 358#endif |
314#if NO_REGEX 315 matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); 316#endif | |
317 } 318 matched = (!(search_type & SRCH_NO_MATCH) && matched) || 319 ((search_type & SRCH_NO_MATCH) && !matched); 320 return (matched); 321} 322 | 359 } 360 matched = (!(search_type & SRCH_NO_MATCH) && matched) || 361 ((search_type & SRCH_NO_MATCH) && !matched); 362 return (matched); 363} 364 |