[BACK]Return to pattern.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / less

Diff for /src/usr.bin/less/pattern.c between version 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2011/09/16 17:47:07 version 1.1.1.2, 2014/04/25 13:33:50
Line 1 
Line 1 
 /*  /*
  * 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.  
  */   */
   
 /*  /*
Line 16 
Line 15 
 #include "pattern.h"  #include "pattern.h"
   
 extern int caseless;  extern int caseless;
   extern int less_is_more;
   
 /*  /*
  * Compile a search pattern, for future use by match_pattern.   * Compile a search pattern, for future use by match_pattern.
Line 26 
Line 26 
         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, 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);
 }  }
   
Line 130 
Line 147 
 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)
Line 167 
Line 190 
 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
Line 182 
Line 208 
 #if HAVE_V8_REGCOMP  #if HAVE_V8_REGCOMP
         return (pattern == NULL);          return (pattern == NULL);
 #endif  #endif
 #if NO_REGEX  
         return (search_pattern != NULL);  
 #endif  
 }  }
   
 /*  /*
Line 236 
Line 259 
         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
Line 252 
Line 278 
         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;
Line 310 
Line 356 
                 *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) ||

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2