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

Re: [PATCH] Pretty-print variable values



Hi,

* Brendan Cully [07-09-05 10:06:40 -0700] wrote:
On Wednesday, 05 September 2007 at 16:29, Rocco Rutte wrote:

+static void pretty_var (char *dst, size_t len, const char *option, const char 
*val)
+{
+  char *p = dst;
+  size_t l = mutt_strlen (option);
+
+  len -= 2; /* save room for "\0 */

Is this safe if len starts out less than 2? Not that it's likely,
but...

I guess not, thanks for the pointer. And while I'm at it, I fixed it to not enforce a trailing " (to make sure it's quoted) since we may not even have a leading " due to space, end accidentially in \ or...

This version also adds pretty printing to the value completion as it was missing there.

I'm going to commit this if there're no further objections.

  bye, Rocco
--
:wq!
diff --git a/init.c b/init.c
index 46c1c22..0fe91d8 100644
--- a/init.c
+++ b/init.c
@@ -1630,6 +1630,49 @@ static void mutt_restore_default (struct option_t *p)
     set_option (OPTREDRAWTREE);
 }
 
+static void pretty_var (char *dst, size_t len, const char *option, const char 
*val)
+{
+  char *p;
+
+  if (!len)
+    return;
+
+  strfcpy (dst, option, len);
+  len--; /* save room for \0 */
+  p = dst + mutt_strlen (dst);
+
+  if (p - dst < len)
+    *p++ = '=';
+  if (p - dst < len)
+    *p++ = '"';
+
+#define ESC_CHAR(C)    do { *p++ = '\\'; if (p - dst < len) *p++ = C; } 
while(0)
+  while (p - dst < len && val && *val)
+  {
+    switch (*val)
+    {
+    case '\n':
+      ESC_CHAR('n');
+      break;
+    case '\r':
+      ESC_CHAR('r');
+      break;
+    case '\t':
+      ESC_CHAR('t');
+      break;
+    default:
+      if (strchr ("\"\\", *val) != NULL && p - dst < len - 1)
+       *p++ = '\\';
+      *p++ = *val;
+    }
+    val++;
+  }
+#undef ESC_CHAR
+  if (p - dst < len)
+    *p++ = '"';
+  *p = 0;
+}
+
 static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
 {
   int query, unset, inv, reset, r = 0;
@@ -1783,7 +1826,7 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned 
long data, BUFFER *err)
         {
           if ((val = myvar_get (myvar)))
           {
-            snprintf (err->data, err->dsize, "%s=\"%s\"", myvar, val);
+           pretty_var (err->data, err->dsize, myvar, val);
             break;
           }
           else
@@ -1798,12 +1841,18 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned 
long data, BUFFER *err)
          rfc822_write_address (_tmp, sizeof (_tmp), *((ADDRESS **) 
MuttVars[idx].data), 0);
          val = _tmp;
        }
+       else if (DTYPE (MuttVars[idx].type) == DT_PATH)
+       {
+         _tmp[0] = '\0';
+         strfcpy (_tmp, *((char **) MuttVars[idx].data), sizeof (_tmp));
+         mutt_pretty_mailbox (_tmp);
+         val = _tmp;
+       }
        else
          val = *((char **) MuttVars[idx].data);
        
        /* user requested the value of this variable */
-       snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
-                 NONULL (val));
+       pretty_var (err->data, err->dsize, MuttVars[idx].option, NONULL(val));
        break;
       }
       else
@@ -1858,8 +1907,7 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned 
long data, BUFFER *err)
       if (query || *s->dptr != '=')
       {
        /* user requested the value of this variable */
-       snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
-                 NONULL (ptr->pattern));
+       pretty_var (err->data, err->dsize, MuttVars[idx].option, 
NONULL(ptr->pattern));
        break;
       }
 
@@ -2532,14 +2580,14 @@ int mutt_var_value_complete (char *buffer, size_t len, 
int pos)
     {
       if ((myvarval = myvar_get(var)) != NULL)
       {
-       snprintf (pt, len - (pt - buffer), "%s=\"%s\"", var, myvarval);
+       pretty_var (pt, len - (pt - buffer), var, myvarval);
        return 1;
       }
       return 0; /* no such variable. */
     }
     else if (var_to_string (idx, val, sizeof (val)))
     {
-      snprintf (pt, len - (pt - buffer), "%s=\"%s\"", var, val);
+      pretty_var (pt, len - (pt - buffer), var, val);
       return 1;
     }
   }