]> jfr.im git - irc/irssi/irssi.git/commitdiff
switch for gregex and regex.h
authorAilin Nemui <redacted>
Tue, 3 Jan 2017 11:04:56 +0000 (12:04 +0100)
committerAilin Nemui <redacted>
Tue, 3 Jan 2017 11:29:11 +0000 (12:29 +0100)
configure.ac
src/core/ignore.c
src/core/ignore.h
src/core/misc.c
src/fe-common/core/fe-ignore.c
src/fe-common/core/hilight-text.c
src/fe-common/core/hilight-text.h
src/fe-text/textbuffer.c

index 7eb1bfe1d6d284ab4aecf61b156fe48e0b755eeb..8d588408eb9e52da6b4f07d9d5bb17aa7e74c2b6 100644 (file)
@@ -144,6 +144,15 @@ AC_ARG_ENABLE(true-color,
        fi,
        want_truecolor=no)
 
+AC_ARG_ENABLE(gregex,
+[  --disable-gregex     Build without GRegex (fall back to regex.h)],
+       if test x$enableval = xno ; then
+               want_gregex=no
+       else
+               want_gregex=yes
+       fi,
+       want_gregex=yes)
+
 dnl **
 dnl ** just some generic stuff...
 dnl **
@@ -534,6 +543,12 @@ else
        want_truecolor=no
 fi
 
+if test "x$want_gregex" = "xyes"; then
+       AC_DEFINE([USE_GREGEX], [], [use GRegex for regular expressions])
+else
+       want_gregex=no
+fi
+
 AH_TEMPLATE(HAVE_GMODULE)
 AH_TEMPLATE(HAVE_SOCKS_H, [misc..])
 AH_TEMPLATE(HAVE_STATIC_PERL)
@@ -648,6 +663,7 @@ echo
 
 echo "Building with 64bit DCC support .. : $offt_64bit"
 echo "Building with true color support.. : $want_truecolor"
+echo "Building with GRegex ............. : $want_gregex"
 
 echo
 echo "If there are any problems, read the INSTALL file."
index cfcbd792e757c8f4526fb476a5ac8b7885792a0f..bf8db04f5b8825c821bd17b7b74d61d814d88944 100644 (file)
@@ -67,8 +67,13 @@ static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
                return FALSE;
 
        if (rec->regexp) {
+#ifdef USE_GREGEX
                return rec->preg != NULL &&
                        g_regex_match(rec->preg, text, 0, NULL);
+#else
+               return rec->regexp_compiled &&
+                       regexec(&rec->preg, text, 0, NULL, 0) == 0;
+#endif
        }
 
        return rec->fullword ?
@@ -322,6 +327,7 @@ static void ignore_remove_config(IGNORE_REC *rec)
 
 static void ignore_init_rec(IGNORE_REC *rec)
 {
+#ifdef USE_GREGEX
        if (rec->preg != NULL)
                g_regex_unref(rec->preg);
 
@@ -335,6 +341,27 @@ static void ignore_init_rec(IGNORE_REC *rec)
                        g_error_free(re_error);
                }
        }
+#else
+       char *errbuf;
+       int errcode, errbuf_len;
+
+       if (rec->regexp_compiled) regfree(&rec->preg);
+       rec->regexp_compiled = FALSE;
+
+       if (rec->regexp && rec->pattern != NULL) {
+               errcode = regcomp(&rec->preg, rec->pattern,
+                               REG_EXTENDED|REG_ICASE|REG_NOSUB);
+               if (errcode != 0) {
+                       errbuf_len = regerror(errcode, &rec->preg, 0, 0);
+                       errbuf = g_malloc(errbuf_len);
+                       regerror(errcode, &rec->preg, errbuf, errbuf_len);
+                       g_warning("Failed to compile regexp '%s': %s", rec->pattern, errbuf);
+                       g_free(errbuf);
+               } else {
+                       rec->regexp_compiled = TRUE;
+               }
+       }
+#endif
 }
 
 void ignore_add_rec(IGNORE_REC *rec)
@@ -354,7 +381,11 @@ static void ignore_destroy(IGNORE_REC *rec, int send_signal)
        if (send_signal)
                signal_emit("ignore destroyed", 1, rec);
 
+#ifdef USE_GREGEX
        if (rec->preg != NULL) g_regex_unref(rec->preg);
+#else
+       if (rec->regexp_compiled) regfree(&rec->preg);
+#endif
        if (rec->channels != NULL) g_strfreev(rec->channels);
        g_free_not_null(rec->mask);
        g_free_not_null(rec->servertag);
index f37f8b2812b78feec96b00249a37f054ebd345e8..80ae1d122729300fe48e32dfb68f1a5048e1ec4f 100644 (file)
@@ -1,6 +1,10 @@
 #ifndef __IGNORE_H
 #define __IGNORE_H
 
+#ifndef USE_GREGEX
+#  include <regex.h>
+#endif
+
 typedef struct _IGNORE_REC IGNORE_REC;
 
 struct _IGNORE_REC {
@@ -16,7 +20,12 @@ struct _IGNORE_REC {
        unsigned int regexp:1;
        unsigned int fullword:1;
        unsigned int replies:1; /* ignore replies to nick in channel */
+#ifdef USE_GREGEX
        GRegex *preg;
+#else
+       unsigned int regexp_compiled:1; /* should always be TRUE, unless regexp is invalid */
+       regex_t preg;
+#endif
 };
 
 extern GSList *ignores;
index c59eb12678da5ca28a1abcae659824bef16d5a19..1cfa15b65f16ff30b4ac288440092b5ffbca5cae 100644 (file)
 #include "misc.h"
 #include "commands.h"
 
+#ifndef USE_GREGEX
+#  include <regex.h>
+#endif
+
 typedef struct {
        int condition;
        GInputFunction function;
index 03fd4dd2b0fb2a37a41e28d2d531fe1ec4b66769..800e881d0f53a418a20466446cf1acae39e17d97 100644 (file)
@@ -58,8 +58,13 @@ static void ignore_print(int index, IGNORE_REC *rec)
                g_string_append(options, "-regexp ");
                if (rec->pattern == NULL)
                        g_string_append(options, "[INVALID! -pattern missing] ");
+#ifdef USE_GREGEX
                else if (rec->preg == NULL)
                        g_string_append(options, "[INVALID!] ");
+#else
+               else if (!rec->regexp_compiled)
+                       g_string_append(options, "[INVALID!] ");
+#endif
        }
        if (rec->fullword) g_string_append(options, "-full ");
        if (rec->replies) g_string_append(options, "-replies ");
index 7a7f473c240bc60a682a4bd0f010ebd541652cff..037cde5c95cb546a593330161e40855f44e50975 100644 (file)
@@ -101,7 +101,11 @@ static void hilight_destroy(HILIGHT_REC *rec)
 {
        g_return_if_fail(rec != NULL);
 
+#ifdef USE_GREGEX
        if (rec->preg != NULL) g_regex_unref(rec->preg);
+#else
+       if (rec->regexp_compiled) regfree(&rec->preg);
+#endif
        if (rec->channels != NULL) g_strfreev(rec->channels);
        g_free_not_null(rec->color);
        g_free_not_null(rec->act_color);
@@ -118,10 +122,19 @@ static void hilights_destroy_all(void)
 
 static void hilight_init_rec(HILIGHT_REC *rec)
 {
+#ifdef USE_GREGEX
        if (rec->preg != NULL)
                g_regex_unref(rec->preg);
 
        rec->preg = g_regex_new(rec->text, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_CASELESS, 0, NULL);
+#else
+       if (rec->regexp_compiled) regfree(&rec->preg);
+       if (!rec->regexp)
+               rec->regexp_compiled = FALSE;
+       else
+               rec->regexp_compiled = regcomp(&rec->preg, rec->text,
+                               rec->case_sensitive ? REG_EXTENDED : (REG_EXTENDED|REG_ICASE)) == 0;
+#endif
 }
 
 void hilight_create(HILIGHT_REC *rec)
@@ -194,6 +207,7 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, const char *text,
        gboolean ret = FALSE;
 
        if (rec->regexp) {
+#ifdef USE_GREGEX
                if (rec->preg != NULL) {
                        GMatchInfo *match;
 
@@ -204,6 +218,19 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, const char *text,
 
                        g_match_info_free(match);
                }
+#else
+               regmatch_t rmatch[1];
+
+               if (rec->regexp_compiled &&
+                       regexec(&rec->preg, text, 1, rmatch, 0) == 0) {
+                       if (rmatch[0].rm_so > 0 &&
+                               match_beg != NULL && match_end != NULL) {
+                               *match_beg = rmatch[0].rm_so;
+                               *match_end = rmatch[0].rm_eo;
+                       }
+                       ret = TRUE;
+               }
+#endif
        } else {
                char *match;
 
@@ -502,8 +529,13 @@ static void hilight_print(int index, HILIGHT_REC *rec)
        if (rec->case_sensitive) g_string_append(options, "-matchcase ");
        if (rec->regexp) {
                g_string_append(options, "-regexp ");
+#ifdef USE_GREGEX
                if (rec->preg == NULL)
                        g_string_append(options, "[INVALID!] ");
+#else
+               if (!rec->regexp_compiled)
+                       g_string_append(options, "[INVALID!] ");
+#endif
        }
 
        if (rec->priority != 0)
index 93c573c2e0195e27157fd2598a47b0d493a174b5..76beec1f3e67345ecb2bd617a41d318848cc400a 100644 (file)
@@ -1,6 +1,10 @@
 #ifndef __HILIGHT_TEXT_H
 #define __HILIGHT_TEXT_H
 
+#ifndef USE_GREGEX
+#  include <regex.h>
+#endif
+
 #include "formats.h"
 
 struct _HILIGHT_REC {
@@ -20,7 +24,12 @@ struct _HILIGHT_REC {
        unsigned int fullword:1; /* match `text' only for full words */
        unsigned int regexp:1; /* `text' is a regular expression */
        unsigned int case_sensitive:1;/* `text' must match case */
+#ifdef USE_GREGEX
        GRegex *preg;
+#else
+       unsigned int regexp_compiled:1; /* should always be TRUE, unless regexp is invalid */
+       regex_t preg;
+#endif
        char *servertag;
 };
 
index 979a610475dd8e39bc45c2bd05f21f013908fd4b..ae4636a5556ef0ae969207de09588d042dce7c2d 100644 (file)
 
 #include "textbuffer.h"
 
+#ifndef USE_GREGEX
+#  include <regex.h>
+#endif
+
 #define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
 
 TEXT_BUFFER_REC *textbuffer_create(void)
@@ -533,7 +537,11 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
                            int before, int after,
                            int regexp, int fullword, int case_sensitive)
 {
+#ifdef USE_GREGEX
        GRegex *preg;
+#else
+       regex_t preg;
+#endif
         LINE_REC *line, *pre_line;
        GList *matches;
        GString *str;
@@ -543,6 +551,7 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
        g_return_val_if_fail(buffer != NULL, NULL);
        g_return_val_if_fail(text != NULL, NULL);
 
+#ifdef USE_GREGEX
        preg = NULL;
 
        if (regexp) {
@@ -551,6 +560,14 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
                if (preg == NULL)
                        return NULL;
        }
+#else
+       if (regexp) {
+               int flags = REG_EXTENDED | REG_NOSUB |
+                       (case_sensitive ? 0 : REG_ICASE);
+               if (regcomp(&preg, text, flags) != 0)
+                       return NULL;
+       }
+#endif
 
        matches = NULL; match_after = 0;
         str = g_string_new(NULL);
@@ -571,8 +588,12 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
 
                        if (line_matched) {
                                line_matched = regexp ?
-                                   g_regex_match(preg, str->str, 0, NULL) :
-                                   match_func(str->str, text) != NULL;
+#ifdef USE_GREGEX
+                                   g_regex_match(preg, str->str, 0, NULL)
+#else
+                                   regexec(&preg, str->str, 0, NULL, 0) == 0
+#endif
+                                       : match_func(str->str, text) != NULL;
                        }
                }
 
@@ -602,8 +623,12 @@ GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
                }
        }
 
+#ifdef USE_GREGEX
        if (preg != NULL)
                g_regex_unref(preg);
+#else
+       if (regexp) regfree(&preg);
+#endif
         g_string_free(str, TRUE);
        return matches;
 }