pattern.c (238730) | pattern.c (293190) |
---|---|
1/* | 1/* |
2 * Copyright (C) 1984-2012 Mark Nudelman | 2 * Copyright (C) 1984-2015 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 * 7 * For more information, see the README file. 8 */ 9 10/* --- 4 unchanged lines hidden (view full) --- 15#include "pattern.h" 16 17extern int caseless; 18 19/* 20 * Compile a search pattern, for future use by match_pattern. 21 */ 22 static int | 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, see the README file. 8 */ 9 10/* --- 4 unchanged lines hidden (view full) --- 15#include "pattern.h" 16 17extern int caseless; 18 19/* 20 * Compile a search pattern, for future use by match_pattern. 21 */ 22 static int |
23compile_pattern2(pattern, search_type, comp_pattern) | 23compile_pattern2(pattern, search_type, comp_pattern, show_error) |
24 char *pattern; 25 int search_type; 26 void **comp_pattern; | 24 char *pattern; 25 int search_type; 26 void **comp_pattern; |
27 int show_error; |
|
27{ 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)) 38 { 39 free(comp); | 28{ 29 if (search_type & SRCH_NO_REGEX) 30 return (0); 31 { 32#if HAVE_GNU_REGEX 33 struct re_pattern_buffer *comp = (struct re_pattern_buffer *) 34 ecalloc(1, sizeof(struct re_pattern_buffer)); 35 struct re_pattern_buffer **pcomp = 36 (struct re_pattern_buffer **) comp_pattern; 37 re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); 38 if (re_compile_pattern(pattern, strlen(pattern), comp)) 39 { 40 free(comp); |
40 error("Invalid pattern", NULL_PARG); | 41 if (show_error) 42 error("Invalid pattern", NULL_PARG); |
41 return (-1); 42 } 43 if (*pcomp != NULL) 44 regfree(*pcomp); 45 *pcomp = comp; 46#endif 47#if HAVE_POSIX_REGCOMP 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); | 43 return (-1); 44 } 45 if (*pcomp != NULL) 46 regfree(*pcomp); 47 *pcomp = comp; 48#endif 49#if HAVE_POSIX_REGCOMP 50 regex_t *comp = (regex_t *) ecalloc(1, sizeof(regex_t)); 51 regex_t **pcomp = (regex_t **) comp_pattern; 52 if (regcomp(comp, pattern, REGCOMP_FLAG)) 53 { 54 free(comp); |
53 error("Invalid pattern", NULL_PARG); | 55 if (show_error) 56 error("Invalid pattern", NULL_PARG); |
54 return (-1); 55 } 56 if (*pcomp != NULL) 57 regfree(*pcomp); 58 *pcomp = comp; 59#endif 60#if HAVE_PCRE 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; | 57 return (-1); 58 } 59 if (*pcomp != NULL) 60 regfree(*pcomp); 61 *pcomp = comp; 62#endif 63#if HAVE_PCRE 64 pcre *comp; 65 pcre **pcomp = (pcre **) comp_pattern; 66 constant char *errstring; 67 int erroffset; 68 PARG parg; 69 comp = pcre_compile(pattern, 0, 70 &errstring, &erroffset, NULL); 71 if (comp == NULL) 72 { 73 parg.p_string = (char *) errstring; |
71 error("%s", &parg); | 74 if (show_error) 75 error("%s", &parg); |
72 return (-1); 73 } 74 *pcomp = comp; 75#endif 76#if HAVE_RE_COMP 77 PARG parg; 78 int *pcomp = (int *) comp_pattern; 79 if ((parg.p_string = re_comp(pattern)) != NULL) 80 { | 76 return (-1); 77 } 78 *pcomp = comp; 79#endif 80#if HAVE_RE_COMP 81 PARG parg; 82 int *pcomp = (int *) comp_pattern; 83 if ((parg.p_string = re_comp(pattern)) != NULL) 84 { |
81 error("%s", &parg); | 85 if (show_error) 86 error("%s", &parg); |
82 return (-1); 83 } 84 *pcomp = 1; 85#endif 86#if HAVE_REGCMP 87 char *comp; 88 char **pcomp = (char **) comp_pattern; 89 if ((comp = regcmp(pattern, 0)) == NULL) 90 { | 87 return (-1); 88 } 89 *pcomp = 1; 90#endif 91#if HAVE_REGCMP 92 char *comp; 93 char **pcomp = (char **) comp_pattern; 94 if ((comp = regcmp(pattern, 0)) == NULL) 95 { |
91 error("Invalid pattern", NULL_PARG); | 96 if (show_error) 97 error("Invalid pattern", NULL_PARG); |
92 return (-1); 93 } 94 if (pcomp != NULL) 95 free(*pcomp); 96 *pcomp = comp; 97#endif 98#if HAVE_V8_REGCOMP 99 struct regexp *comp; 100 struct regexp **pcomp = (struct regexp **) comp_pattern; | 98 return (-1); 99 } 100 if (pcomp != NULL) 101 free(*pcomp); 102 *pcomp = comp; 103#endif 104#if HAVE_V8_REGCOMP 105 struct regexp *comp; 106 struct regexp **pcomp = (struct regexp **) comp_pattern; |
101 if ((comp = regcomp(pattern)) == NULL) | 107 reg_show_error = show_error; 108 comp = regcomp(pattern); 109 reg_show_error = 1; 110 if (comp == NULL) |
102 { 103 /* 104 * regcomp has already printed an error message 105 * via regerror(). 106 */ 107 return (-1); 108 } 109 if (*pcomp != NULL) --- 18 unchanged lines hidden (view full) --- 128 129 if (caseless != OPT_ONPLUS) 130 cvt_pattern = pattern; 131 else 132 { 133 cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC)); 134 cvt_text(cvt_pattern, pattern, (int *)NULL, (int *)NULL, CVT_TO_LC); 135 } | 111 { 112 /* 113 * regcomp has already printed an error message 114 * via regerror(). 115 */ 116 return (-1); 117 } 118 if (*pcomp != NULL) --- 18 unchanged lines hidden (view full) --- 137 138 if (caseless != OPT_ONPLUS) 139 cvt_pattern = pattern; 140 else 141 { 142 cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC)); 143 cvt_text(cvt_pattern, pattern, (int *)NULL, (int *)NULL, CVT_TO_LC); 144 } |
136 result = compile_pattern2(cvt_pattern, search_type, comp_pattern); | 145 result = compile_pattern2(cvt_pattern, search_type, comp_pattern, 1); |
137 if (cvt_pattern != pattern) 138 free(cvt_pattern); 139 return (result); 140} 141 142/* 143 * Forget that we have a compiled pattern. 144 */ --- 33 unchanged lines hidden (view full) --- 178 struct regexp **pcomp = (struct regexp **) pattern; 179 if (*pcomp != NULL) 180 free(*pcomp); 181 *pcomp = NULL; 182#endif 183} 184 185/* | 146 if (cvt_pattern != pattern) 147 free(cvt_pattern); 148 return (result); 149} 150 151/* 152 * Forget that we have a compiled pattern. 153 */ --- 33 unchanged lines hidden (view full) --- 187 struct regexp **pcomp = (struct regexp **) pattern; 188 if (*pcomp != NULL) 189 free(*pcomp); 190 *pcomp = NULL; 191#endif 192} 193 194/* |
195 * Can a pattern be successfully compiled? 196 */ 197 public int 198valid_pattern(pattern) 199 char *pattern; 200{ 201 void *comp_pattern; 202 int result; 203 204 CLEAR_PATTERN(comp_pattern); 205 result = compile_pattern2(pattern, 0, &comp_pattern, 0); 206 if (result != 0) 207 return (0); 208 uncompile_pattern(&comp_pattern); 209 return (1); 210} 211 212/* |
|
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); --- 8 unchanged lines hidden (view full) --- 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 | 213 * Is a compiled pattern null? 214 */ 215 public int 216is_null_pattern(pattern) 217 void *pattern; 218{ 219#if HAVE_GNU_REGEX 220 return (pattern == NULL); --- 8 unchanged lines hidden (view full) --- 229 return (pattern == 0); 230#endif 231#if HAVE_REGCMP 232 return (pattern == NULL); 233#endif 234#if HAVE_V8_REGCOMP 235 return (pattern == NULL); 236#endif |
237#if NO_REGEX 238 return (pattern == NULL); 239#endif |
|
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) --- 4 unchanged lines hidden (view full) --- 222 char **pfound, **pend; 223{ 224 register char *pp, *lp; 225 register char *pattern_end = pattern + pattern_len; 226 register char *buf_end = buf + buf_len; 227 228 for ( ; buf < buf_end; buf++) 229 { | 240} 241 242/* 243 * Simple pattern matching function. 244 * It supports no metacharacters like *, etc. 245 */ 246 static int 247match(pattern, pattern_len, buf, buf_len, pfound, pend) --- 4 unchanged lines hidden (view full) --- 252 char **pfound, **pend; 253{ 254 register char *pp, *lp; 255 register char *pattern_end = pattern + pattern_len; 256 register char *buf_end = buf + buf_len; 257 258 for ( ; buf < buf_end; buf++) 259 { |
230 for (pp = pattern, lp = buf; *pp == *lp; pp++, lp++) | 260 for (pp = pattern, lp = buf; ; pp++, lp++) 261 { 262 char cp = *pp; 263 char cl = *lp; 264 if (caseless == OPT_ONPLUS && ASCII_IS_UPPER(cp)) 265 cp = ASCII_TO_LOWER(cp); 266 if (cp != cl) 267 break; |
231 if (pp == pattern_end || lp == buf_end) 232 break; | 268 if (pp == pattern_end || lp == buf_end) 269 break; |
270 } |
|
233 if (pp == pattern_end) 234 { 235 if (pfound != NULL) 236 *pfound = buf; 237 if (pend != NULL) 238 *pend = lp; 239 return (1); 240 } --- 31 unchanged lines hidden (view full) --- 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 | 271 if (pp == pattern_end) 272 { 273 if (pfound != NULL) 274 *pfound = buf; 275 if (pend != NULL) 276 *pend = lp; 277 return (1); 278 } --- 31 unchanged lines hidden (view full) --- 310#endif 311#if HAVE_REGCMP 312 char *spattern = (char *) pattern; 313#endif 314#if HAVE_V8_REGCOMP 315 struct regexp *spattern = (struct regexp *) pattern; 316#endif 317 |
318 *sp = *ep = NULL; |
|
280#if NO_REGEX 281 search_type |= SRCH_NO_REGEX; 282#endif 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; | 319#if NO_REGEX 320 search_type |= SRCH_NO_REGEX; 321#endif 322 if (search_type & SRCH_NO_REGEX) 323 matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep); 324 else 325 { 326#if HAVE_GNU_REGEX 327 { 328 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; | 329 spattern->not_bol = notbol; |
293 re_set_registers(spattern, &search_regs, 1, starts, ends); | 330 spattern->regs_allocated = REGS_UNALLOCATED; |
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 } | 331 matched = re_search(spattern, line, line_len, 0, line_len, &search_regs) >= 0; 332 if (matched) 333 { 334 *sp = line + search_regs.start[0]; 335 *ep = line + search_regs.end[0]; 336 } |
300 free(starts); 301 free(ends); | |
302 } 303#endif 304#if HAVE_POSIX_REGCOMP 305 { 306 regmatch_t rm; 307 int flags = (notbol) ? REG_NOTBOL : 0; | 337 } 338#endif 339#if HAVE_POSIX_REGCOMP 340 { 341 regmatch_t rm; 342 int flags = (notbol) ? REG_NOTBOL : 0; |
343#ifdef REG_STARTEND 344 flags |= REG_STARTEND; 345 rm.rm_so = 0; 346 rm.rm_eo = line_len; 347#endif |
|
308 matched = !regexec(spattern, line, 1, &rm, flags); 309 if (matched) 310 { 311#ifndef __WATCOMC__ 312 *sp = line + rm.rm_so; 313 *ep = line + rm.rm_eo; 314#else 315 *sp = rm.rm_sp; --- 49 unchanged lines hidden --- | 348 matched = !regexec(spattern, line, 1, &rm, flags); 349 if (matched) 350 { 351#ifndef __WATCOMC__ 352 *sp = line + rm.rm_so; 353 *ep = line + rm.rm_eo; 354#else 355 *sp = rm.rm_sp; --- 49 unchanged lines hidden --- |