<<< Date Index >>>     <<< Thread Index >>>

Re: [PATCH] expand mutt vars as we do environment vars



On 2006-01-12 at 15:42 +0100, Michael Tatge wrote:
> tab complete for own vars would be nice.

Try this, which works for me and includes my second version of the
myvar_del() fix.

Theoretically allows an arbitrary number of my_vars without risking
buffer overflow of Matches by moving it to a dynamic array; does mutt
get tested with memory-leak detectors, since this one static pointer
will hold memory not freed before process exit.

Test of memory reallocation was to set my_a through my_j (or further)
and type
  :set ?<TAB>
because space is pre-allocated to hold 10 my_* variables (there are more
variables than commands).

--- mutt/init.c 2006-01-12 18:24:01.000000000 +0100
+++ mutt-tabs/init.c    2006-01-12 19:04:04.000000000 +0100
@@ -2271,7 +2271,25 @@
 
 int  Num_matched = 0; /* Number of matches for completion */
 char Completed [STRING] = {0}; /* completed string (command or variable) */
-char *Matches[MAX(NUMVARS,NUMCOMMANDS) + 1]; /* all the matches + User_typed */
+const char **Matches;
+/* this is a lie until mutt_init runs: */
+int  Matches_listsize = MAX(NUMVARS,NUMCOMMANDS) + 10;
+
+static void matches_ensure_morespace(int current)
+{
+  int base_space, extra_space, space;
+
+  if (current > Matches_listsize - 2)
+  {
+    base_space = MAX(NUMVARS,NUMCOMMANDS) + 1; 
+    extra_space = Matches_listsize - base_space;
+    extra_space *= 2;
+    space = base_space + extra_space;
+    safe_realloc (&Matches, space * sizeof (char *));
+    memset (&Matches[current + 1], 0, space - current);
+    Matches_listsize = space;
+  }
+}
 
 /* helper function for completion.  Changes the dest buffer if
    necessary/possible to aid completion.
@@ -2280,12 +2298,13 @@
        try == user entered data for completion.
        len == length of dest buffer.
 */
-static void candidate (char *dest, char *try, char *src, int len)
+static void candidate (char *dest, char *try, const char *src, int len)
 {
   int l;
 
   if (strstr (src, try) == src)
   {
+    matches_ensure_morespace (Num_matched);
     Matches[Num_matched++] = src;
     if (dest[0] == 0)
       strfcpy (dest, src, len);
@@ -2302,6 +2321,7 @@
   char *pt = buffer;
   int num;
   int spaces; /* keep track of the number of leading spaces on the line */
+  myvar_t *myv;
 
   SKIPWS (buffer);
   spaces = buffer - pt;
@@ -2317,10 +2337,11 @@
     {
       Num_matched = 0;
       strfcpy (User_typed, pt, sizeof (User_typed));
-      memset (Matches, 0, sizeof (Matches));
+      memset (Matches, 0, Matches_listsize);
       memset (Completed, 0, sizeof (Completed));
       for (num = 0; Commands[num].name; num++)
        candidate (Completed, User_typed, Commands[num].name, sizeof 
(Completed));
+      matches_ensure_morespace (Num_matched);
       Matches[Num_matched++] = User_typed;
 
       /* All matches are stored. Longest non-ambiguous string is ""
@@ -2370,10 +2391,13 @@
     {
       Num_matched = 0;
       strfcpy (User_typed, pt, sizeof (User_typed));
-      memset (Matches, 0, sizeof (Matches));
+      memset (Matches, 0, Matches_listsize);
       memset (Completed, 0, sizeof (Completed));
       for (num = 0; MuttVars[num].option; num++)
        candidate (Completed, User_typed, MuttVars[num].option, sizeof 
(Completed));
+      for (myv = MyVars; myv; myv = myv->next)
+       candidate (Completed, User_typed, myv->name, sizeof (Completed));
+      matches_ensure_morespace (Num_matched);
       Matches[Num_matched++] = User_typed;
 
       /* All matches are stored. Longest non-ambiguous string is ""
@@ -2409,7 +2433,7 @@
     {
       Num_matched = 0;
       strfcpy (User_typed, pt, sizeof (User_typed));
-      memset (Matches, 0, sizeof (Matches));
+      memset (Matches, 0, Matches_listsize);
       memset (Completed, 0, sizeof (Completed));
       for (num = 0; menu[num].name; num++)
        candidate (Completed, User_typed, menu[num].name, sizeof (Completed));
@@ -2420,6 +2444,7 @@
        for (num = 0; menu[num].name; num++)
          candidate (Completed, User_typed, menu[num].name, sizeof (Completed));
       }
+      matches_ensure_morespace (Num_matched);
       Matches[Num_matched++] = User_typed;
 
       /* All matches are stored. Longest non-ambiguous string is ""
@@ -2852,6 +2877,7 @@
   mutt_set_langinfo_charset ();
   mutt_set_charset (Charset);
   
+  Matches = safe_calloc (Matches_listsize, sizeof (char *));
   
   /* Set standard defaults */
   for (i = 0; MuttVars[i].option; i++)
@@ -3054,7 +3080,10 @@
   {
     if (!mutt_strcmp (cur->name, var))
     {
-      prev->next = cur->next;
+      if (cur == MyVars)
+       MyVars = cur->next;
+      else
+       prev->next = cur->next;
       FREE (&cur->name);
       FREE (&cur->value);
       FREE (&cur);