"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/regex.c" (12 Oct 2016, 5482 Bytes) of package /linux/misc/tin-2.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "regex.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : regex.c
    4  *  Author    : Jason Faultless <jason@altarstone.com>
    5  *  Created   : 1997-02-21
    6  *  Updated   : 2011-11-09
    7  *  Notes     : Regular expression subroutines
    8  *  Credits   :
    9  *
   10  * Copyright (c) 1997-2017 Jason Faultless <jason@altarstone.com>
   11  * All rights reserved.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. The name of the author may not be used to endorse or promote
   22  *    products derived from this software without specific prior written
   23  *    permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   26  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   29  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   31  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   36  */
   37 
   38 
   39 #ifndef TIN_H
   40 #   include "tin.h"
   41 #endif /* !TIN_H */
   42 #ifndef TCURSES_H
   43 #   include "tcurses.h"
   44 #endif /* !TCURSES_H */
   45 
   46 /*
   47  * See if pattern is matched in string. Return TRUE or FALSE
   48  * if icase=TRUE then ignore case in the compare
   49  * if a precompiled regex is provided it will be used instead of pattern
   50  *
   51  * If you use match_regex() with full regexes within a loop you should always
   52  * provide a precompiled error because if the compilation of the regex fails
   53  * an error message will be display on each execution of match_regex()
   54  */
   55 t_bool
   56 match_regex(
   57     const char *string,
   58     char *pattern,
   59     struct regex_cache *cache,
   60     t_bool icase)
   61 {
   62     int error;
   63     struct regex_cache tmp_cache = { NULL, NULL };
   64     struct regex_cache *ptr_cache;
   65 
   66     if (!tinrc.wildcard)    /* wildmat matching */
   67         return wildmat(string, pattern, icase);
   68 
   69     /* full regexes */
   70     if (cache != NULL && cache->re != NULL)
   71         ptr_cache = cache;  /* use the provided regex cache */
   72     else {
   73         /* compile the regex internally */
   74         if (!compile_regex(pattern, &tmp_cache, (icase ? PCRE_CASELESS : 0)))
   75             return FALSE;
   76         ptr_cache = &tmp_cache;
   77     }
   78 
   79     if ((error = pcre_exec(ptr_cache->re, ptr_cache->extra, string, strlen(string), 0, 0, NULL, 0)) >= 0) {
   80         if (ptr_cache == &tmp_cache) {
   81             FreeIfNeeded(tmp_cache.re);
   82             FreeIfNeeded(tmp_cache.extra);
   83         }
   84 
   85         return TRUE;
   86     }
   87 
   88 #if 0
   89     /*
   90      * match_regex() is mostly used within loops and we don't want to display
   91      * an error message on each call
   92      */
   93     if (error != PCRE_ERROR_NOMATCH)
   94         error_message(2, _(txt_pcre_error_num), error);
   95 #endif /* 0 */
   96 
   97     FreeIfNeeded(tmp_cache.re);
   98     FreeIfNeeded(tmp_cache.extra);
   99 
  100     return FALSE;
  101 }
  102 
  103 
  104 /*
  105  * Compile and optimise 'regex'. Return TRUE if all went well
  106  */
  107 t_bool
  108 compile_regex(
  109     const char *regex,
  110     struct regex_cache *cache,
  111     int options)
  112 {
  113     const char *regex_errmsg = 0;
  114     int regex_errpos, my_options = options;
  115 
  116 #if defined(MULTIBYTE_ABLE) && !defined(NO_LOCALE) && (defined(PCRE_MAJOR) && PCRE_MAJOR >= 4)
  117     if (IS_LOCAL_CHARSET("UTF-8")) {
  118         int i;
  119 
  120         pcre_config(PCRE_CONFIG_UTF8, &i);
  121         if (i)
  122             my_options |= PCRE_UTF8;
  123     }
  124 #endif /* MULTIBYTE_ABLE && !NO_LOCALE && PCRE_MAJOR && PCRE_MAJOR >= 4 */
  125 
  126     if ((cache->re = pcre_compile(regex, my_options, &regex_errmsg, &regex_errpos, NULL)) == NULL)
  127         error_message(2, _(txt_pcre_error_at), regex_errmsg, regex_errpos, regex);
  128     else {
  129         cache->extra = pcre_study(cache->re, 0, &regex_errmsg);
  130         if (regex_errmsg != NULL) {
  131             /* we failed, clean up */
  132             FreeAndNull(cache->re);
  133             error_message(2, _(txt_pcre_error_text), regex_errmsg);
  134         } else
  135             return TRUE;
  136     }
  137     return FALSE;
  138 }
  139 
  140 
  141 /*
  142  * Highlight any string on 'row' that match 'regex'
  143  */
  144 void
  145 highlight_regexes(
  146     int row,
  147     struct regex_cache *regex,
  148     int color)
  149 {
  150     char *ptr;
  151     int offsets[6]; /* we are not interrested in any supatterns, so 6 is sufficient */
  152     int offsets_size = ARRAY_SIZE(offsets);
  153 #ifdef USE_CURSES
  154     char buf[LEN];
  155 #else
  156     char *buf;
  157 #endif /* USE_CURSES */
  158 
  159     /* Get contents of line from the screen */
  160 #ifdef USE_CURSES
  161     screen_contents(row, 0, buf);
  162 #else
  163     buf = screen[row].col;
  164 #endif /* USE_CURSES */
  165     ptr = buf;
  166 
  167     /* also check for 0 as offsets[] might be too small to hold all captured subpatterns */
  168     while (pcre_exec(regex->re, regex->extra, ptr, strlen(ptr), 0, 0, offsets, offsets_size) >= 0) {
  169         /* we have a match */
  170         if (color >= 0) /* color the matching text */
  171             word_highlight_string(row, (ptr - buf) + offsets[0], offsets[1] - offsets[0], color);
  172         else
  173             /* inverse the matching text */
  174             highlight_string(row, (ptr - buf) + offsets[0], offsets[1] - offsets[0]);
  175 
  176         if (!tinrc.word_h_display_marks) {
  177 #ifdef USE_CURSES
  178             screen_contents(row, 0, buf);
  179 #endif /* USE_CURSES */
  180             ptr += offsets[1] - 2;
  181         } else
  182             ptr += offsets[1];
  183     }
  184 }