@@ -178,26 +178,23 @@ static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, st
178178
179179 case GREP_PATTERN_TYPE_BRE :
180180 opt -> fixed = 0 ;
181- opt -> pcre = 0 ;
182- opt -> regflags &= ~REG_EXTENDED ;
181+ opt -> pcre1 = 0 ;
183182 break ;
184183
185184 case GREP_PATTERN_TYPE_ERE :
186185 opt -> fixed = 0 ;
187- opt -> pcre = 0 ;
186+ opt -> pcre1 = 0 ;
188187 opt -> regflags |= REG_EXTENDED ;
189188 break ;
190189
191190 case GREP_PATTERN_TYPE_FIXED :
192191 opt -> fixed = 1 ;
193- opt -> pcre = 0 ;
194- opt -> regflags &= ~REG_EXTENDED ;
192+ opt -> pcre1 = 0 ;
195193 break ;
196194
197195 case GREP_PATTERN_TYPE_PCRE :
198196 opt -> fixed = 0 ;
199- opt -> pcre = 1 ;
200- opt -> regflags &= ~REG_EXTENDED ;
197+ opt -> pcre1 = 1 ;
201198 break ;
202199 }
203200}
@@ -324,40 +321,64 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
324321 die ("%s'%s': %s" , where , p -> pattern , error );
325322}
326323
327- #ifdef USE_LIBPCRE
328- static void compile_pcre_regexp (struct grep_pat * p , const struct grep_opt * opt )
324+ static int is_fixed (const char * s , size_t len )
325+ {
326+ size_t i ;
327+
328+ for (i = 0 ; i < len ; i ++ ) {
329+ if (is_regex_special (s [i ]))
330+ return 0 ;
331+ }
332+
333+ return 1 ;
334+ }
335+
336+ static int has_null (const char * s , size_t len )
337+ {
338+ /*
339+ * regcomp cannot accept patterns with NULs so when using it
340+ * we consider any pattern containing a NUL fixed.
341+ */
342+ if (memchr (s , 0 , len ))
343+ return 1 ;
344+
345+ return 0 ;
346+ }
347+
348+ #ifdef USE_LIBPCRE1
349+ static void compile_pcre1_regexp (struct grep_pat * p , const struct grep_opt * opt )
329350{
330351 const char * error ;
331352 int erroffset ;
332353 int options = PCRE_MULTILINE ;
333354
334355 if (opt -> ignore_case ) {
335356 if (has_non_ascii (p -> pattern ))
336- p -> pcre_tables = pcre_maketables ();
357+ p -> pcre1_tables = pcre_maketables ();
337358 options |= PCRE_CASELESS ;
338359 }
339360 if (is_utf8_locale () && has_non_ascii (p -> pattern ))
340361 options |= PCRE_UTF8 ;
341362
342- p -> pcre_regexp = pcre_compile (p -> pattern , options , & error , & erroffset ,
343- p -> pcre_tables );
344- if (!p -> pcre_regexp )
363+ p -> pcre1_regexp = pcre_compile (p -> pattern , options , & error , & erroffset ,
364+ p -> pcre1_tables );
365+ if (!p -> pcre1_regexp )
345366 compile_regexp_failed (p , error );
346367
347- p -> pcre_extra_info = pcre_study (p -> pcre_regexp , 0 , & error );
348- if (!p -> pcre_extra_info && error )
368+ p -> pcre1_extra_info = pcre_study (p -> pcre1_regexp , 0 , & error );
369+ if (!p -> pcre1_extra_info && error )
349370 die ("%s" , error );
350371}
351372
352- static int pcrematch (struct grep_pat * p , const char * line , const char * eol ,
373+ static int pcre1match (struct grep_pat * p , const char * line , const char * eol ,
353374 regmatch_t * match , int eflags )
354375{
355376 int ovector [30 ], ret , flags = 0 ;
356377
357378 if (eflags & REG_NOTBOL )
358379 flags |= PCRE_NOTBOL ;
359380
360- ret = pcre_exec (p -> pcre_regexp , p -> pcre_extra_info , line , eol - line ,
381+ ret = pcre_exec (p -> pcre1_regexp , p -> pcre1_extra_info , line , eol - line ,
361382 0 , flags , ovector , ARRAY_SIZE (ovector ));
362383 if (ret < 0 && ret != PCRE_ERROR_NOMATCH )
363384 die ("pcre_exec failed with error code %d" , ret );
@@ -370,55 +391,36 @@ static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
370391 return ret ;
371392}
372393
373- static void free_pcre_regexp (struct grep_pat * p )
394+ static void free_pcre1_regexp (struct grep_pat * p )
374395{
375- pcre_free (p -> pcre_regexp );
376- pcre_free (p -> pcre_extra_info );
377- pcre_free ((void * )p -> pcre_tables );
396+ pcre_free (p -> pcre1_regexp );
397+ pcre_free (p -> pcre1_extra_info );
398+ pcre_free ((void * )p -> pcre1_tables );
378399}
379- #else /* !USE_LIBPCRE */
380- static void compile_pcre_regexp (struct grep_pat * p , const struct grep_opt * opt )
400+ #else /* !USE_LIBPCRE1 */
401+ static void compile_pcre1_regexp (struct grep_pat * p , const struct grep_opt * opt )
381402{
382403 die ("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE" );
383404}
384405
385- static int pcrematch (struct grep_pat * p , const char * line , const char * eol ,
406+ static int pcre1match (struct grep_pat * p , const char * line , const char * eol ,
386407 regmatch_t * match , int eflags )
387408{
388409 return 1 ;
389410}
390411
391- static void free_pcre_regexp (struct grep_pat * p )
412+ static void free_pcre1_regexp (struct grep_pat * p )
392413{
393414}
394- #endif /* !USE_LIBPCRE */
395-
396- static int is_fixed (const char * s , size_t len )
397- {
398- size_t i ;
399-
400- /* regcomp cannot accept patterns with NULs so we
401- * consider any pattern containing a NUL fixed.
402- */
403- if (memchr (s , 0 , len ))
404- return 1 ;
405-
406- for (i = 0 ; i < len ; i ++ ) {
407- if (is_regex_special (s [i ]))
408- return 0 ;
409- }
410-
411- return 1 ;
412- }
415+ #endif /* !USE_LIBPCRE1 */
413416
414417static void compile_fixed_regexp (struct grep_pat * p , struct grep_opt * opt )
415418{
416419 struct strbuf sb = STRBUF_INIT ;
417420 int err ;
418- int regflags ;
421+ int regflags = opt -> regflags ;
419422
420423 basic_regex_quote_buf (& sb , p -> pattern );
421- regflags = opt -> regflags & ~REG_EXTENDED ;
422424 if (opt -> ignore_case )
423425 regflags |= REG_ICASE ;
424426 err = regcomp (& p -> regexp , sb .buf , regflags );
@@ -455,7 +457,9 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
455457 * simple string match using kws. p->fixed tells us if we
456458 * want to use kws.
457459 */
458- if (opt -> fixed || is_fixed (p -> pattern , p -> patternlen ))
460+ if (opt -> fixed ||
461+ has_null (p -> pattern , p -> patternlen ) ||
462+ is_fixed (p -> pattern , p -> patternlen ))
459463 p -> fixed = !icase || ascii_only ;
460464 else
461465 p -> fixed = 0 ;
@@ -475,8 +479,8 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
475479 return ;
476480 }
477481
478- if (opt -> pcre ) {
479- compile_pcre_regexp (p , opt );
482+ if (opt -> pcre1 ) {
483+ compile_pcre1_regexp (p , opt );
480484 return ;
481485 }
482486
@@ -832,8 +836,8 @@ void free_grep_patterns(struct grep_opt *opt)
832836 case GREP_PATTERN_BODY :
833837 if (p -> kws )
834838 kwsfree (p -> kws );
835- else if (p -> pcre_regexp )
836- free_pcre_regexp (p );
839+ else if (p -> pcre1_regexp )
840+ free_pcre1_regexp (p );
837841 else
838842 regfree (& p -> regexp );
839843 free (p -> pattern );
@@ -912,8 +916,8 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
912916
913917 if (p -> fixed )
914918 hit = !fixmatch (p , line , eol , match );
915- else if (p -> pcre_regexp )
916- hit = !pcrematch (p , line , eol , match , eflags );
919+ else if (p -> pcre1_regexp )
920+ hit = !pcre1match (p , line , eol , match , eflags );
917921 else
918922 hit = !regexec_buf (& p -> regexp , line , eol - line , 1 , match ,
919923 eflags );
0 commit comments