]> granicus.if.org Git - neomutt/commitdiff
Refactor of the regex parsing code into its own function
authorGuyzmo <guyzmo+github+pub@m0g.net>
Mon, 16 Jan 2017 12:53:52 +0000 (13:53 +0100)
committerRichard Russon <rich@flatcap.org>
Sat, 11 Mar 2017 19:10:59 +0000 (19:10 +0000)
Signed-off-by: Guyzmo <guyzmo+github+pub@m0g.net>
init.c
myvar.h

diff --git a/init.c b/init.c
index e0aaf4b38c7e85cf0e2845cd48ee1502ac9c8a57..4f1e69c1a3e5fa51541589c4f5f86bac85f9ed2b 100644 (file)
--- a/init.c
+++ b/init.c
@@ -132,7 +132,7 @@ int query_quadoption (int opt, const char *prompt)
 
 /* given the variable ``s'', return the index into the rc_vars array which
    matches, or -1 if the variable is not found.  */
-int mutt_option_index (char *s)
+int mutt_option_index (const char *s)
 {
   int i;
 
@@ -142,6 +142,70 @@ int mutt_option_index (char *s)
   return (-1);
 }
 
+const struct option_t *mutt_option_get (const char* s)
+{
+  mutt_debug(2, " * mutt_option_get(%s)\n", s);
+  int idx = mutt_option_index(s);
+  if (idx != -1)
+    return &MuttVars[idx];
+  return NULL;
+}
+
+static int parse_regex (int idx, BUFFER *tmp, BUFFER *err) {
+  int e, flags = 0;
+  const char *p;
+  regex_t *rx;
+  REGEXP *ptr = (REGEXP *) MuttVars[idx].data;
+
+  if (!ptr->pattern || mutt_strcmp (ptr->pattern, tmp->data) != 0)
+  {
+    int not = 0;
+
+    /* $mask is case-sensitive */
+    if (mutt_strcmp (MuttVars[idx].option, "mask") != 0)
+      flags |= mutt_which_case (tmp->data);
+
+    p = tmp->data;
+    if (mutt_strcmp (MuttVars[idx].option, "mask") == 0)
+    {
+      if (*p == '!')
+      {
+        not = 1;
+        p++;
+      }
+    }
+
+    rx = safe_malloc (sizeof (regex_t));
+    if ((e = REGCOMP (rx, p, flags)) != 0)
+    {
+      regerror (e, rx, err->data, err->dsize);
+      FREE (&rx);
+      return 0;
+    }
+
+    /* get here only if everything went smoothly */
+    if (ptr->pattern)
+    {
+      FREE (&ptr->pattern);
+      regfree ((regex_t *) ptr->rx);
+      FREE (&ptr->rx);
+    }
+
+    ptr->pattern = safe_strdup (tmp->data);
+    ptr->rx = rx;
+    ptr->not = not;
+
+    return 1;
+  }
+  return 0;
+}
+
+int mutt_option_set (const struct option_t* val, BUFFER *err)
+{
+  mutt_debug(2, " * mutt_option_set()\n");
+  return 0;
+}
+
 static void mutt_free_opt (struct option_t* p)
 {
   REGEXP* pp;
@@ -2178,13 +2242,10 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
     }
     else if (DTYPE(MuttVars[idx].type) == DT_RX)
     {
-      REGEXP *ptr = (REGEXP *) MuttVars[idx].data;
-      regex_t *rx;
-      int e, flags = 0;
-
       if (query || *s->dptr != '=')
       {
        /* user requested the value of this variable */
+        REGEXP *ptr = (REGEXP *) MuttVars[idx].data;
        pretty_var (err->data, err->dsize, MuttVars[idx].option, NONULL(ptr->pattern));
        break;
       }
@@ -2202,46 +2263,8 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
       /* copy the value of the string */
       mutt_extract_token (tmp, s, 0);
 
-      if (!ptr->pattern || mutt_strcmp (ptr->pattern, tmp->data) != 0)
-      {
-       int not = 0;
-
-       /* $mask is case-sensitive */
-       if (mutt_strcmp (MuttVars[idx].option, "mask") != 0)
-         flags |= mutt_which_case (tmp->data);
-
-       p = tmp->data;
-       if (mutt_strcmp (MuttVars[idx].option, "mask") == 0)
-       {
-         if (*p == '!')
-         {
-           not = 1;
-           p++;
-         }
-       }
-         
-       rx = safe_malloc (sizeof (regex_t));
-       if ((e = REGCOMP (rx, p, flags)) != 0)
-       {
-         regerror (e, rx, err->data, err->dsize);
-         FREE (&rx);
-         break;
-       }
-
-       /* get here only if everything went smootly */
-       if (ptr->pattern)
-       {
-         FREE (&ptr->pattern);
-         regfree ((regex_t *) ptr->rx);
-         FREE (&ptr->rx);
-       }
-
-       ptr->pattern = safe_strdup (tmp->data);
-       ptr->rx = rx;
-       ptr->not = not;
-
+      if (parse_regex(idx, tmp, err))
        /* $reply_regexp and $alterantes require special treatment */
-       
        if (Context && Context->msgcount &&
            mutt_strcmp (MuttVars[idx].option, "reply_regexp") == 0)
        {
@@ -2261,7 +2284,6 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
          }
 #undef CUR_ENV
        }
-      }
     }
     else if (DTYPE(MuttVars[idx].type) == DT_MAGIC)
     {
diff --git a/myvar.h b/myvar.h
index b7137133bb6725abcef087f7a84a14333b0ff957..3f2175bbd2b0630d530de44ac7cf75784fec81cc 100644 (file)
--- a/myvar.h
+++ b/myvar.h
@@ -21,7 +21,7 @@
 
 const char* myvar_get (const char* var);
 int var_to_string (int idx, char* val, size_t len);
-int mutt_option_index (char *s);
+int mutt_option_index (const char *s);
 
 #endif