]> granicus.if.org Git - mutt/commitdiff
Convert mutt_complete() to use the buffer pool.
authorKevin McCarthy <kevin@8t8.us>
Fri, 20 Sep 2019 01:52:24 +0000 (18:52 -0700)
committerKevin McCarthy <kevin@8t8.us>
Fri, 20 Sep 2019 02:14:03 +0000 (19:14 -0700)
Add helper functions mutt_buffer_substrcpy() and
mutt_buffer_concatn_path().

Remove mutt_concatn_path() because mutt_complete() was the only
caller.

buffer.c
buffer.h
complete.c
imap/imap.c
imap/imap.h
lib.c
lib.h
muttlib.c
protos.h

index c527de92c48a4d939f706ab80587187bba59d984..c15c30dbae75a8592022f82d5c6d618a1dd46e12 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -208,6 +208,12 @@ void mutt_buffer_strcpy_n (BUFFER *buf, const char *s, size_t len)
   mutt_buffer_addstr_n (buf, s, len);
 }
 
+void mutt_buffer_substrcpy (BUFFER *buf, const char *beg, const char *end)
+{
+  mutt_buffer_clear (buf);
+  if (end > beg)
+    mutt_buffer_strcpy_n (buf, beg, end - beg);
+}
 
 static void increase_buffer_pool (void)
 {
index 72f7fdca23e7f411f6975cc0f81fa6c1f3597f19..6c053ccea32986d368d37f7dd83551b8a5d7f9c2 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -46,6 +46,7 @@ void mutt_buffer_fix_dptr (BUFFER *);
 int mutt_buffer_printf (BUFFER*, const char*, ...);
 void mutt_buffer_strcpy (BUFFER *, const char *);
 void mutt_buffer_strcpy_n (BUFFER *, const char *, size_t);
+void mutt_buffer_substrcpy (BUFFER *buf, const char *beg, const char *end);
 
 /* These append to the buffer. */
 int mutt_buffer_add_printf (BUFFER*, const char*, ...);
index 5ec5459924982826cea217abd1271f92dd3c65e6..e2f8df286d3242490623863e51a8b867b4d6ccb7 100644 (file)
@@ -44,13 +44,18 @@ int mutt_complete (char *s, size_t slen)
   struct dirent *de;
   int i ,init=0;
   size_t len;
-  char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
-  char filepart[_POSIX_PATH_MAX];
+  BUFFER *dirpart = NULL;
+  BUFFER *exp_dirpart = NULL;
+  BUFFER *filepart = NULL;
+  BUFFER *buf = NULL;
+
 #ifdef USE_IMAP
-  char imap_path[LONG_STRING];
+  BUFFER *imap_path = NULL;
+  int rc;
 
   dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
 
+  imap_path = mutt_buffer_pool_get ();
   /* we can use '/' as a delimiter, imap_complete rewrites it */
   if (*s == '=' || *s == '+' || *s == '!')
   {
@@ -59,38 +64,45 @@ int mutt_complete (char *s, size_t slen)
     else
       p = NONULL (Maildir);
 
-    mutt_concat_path (imap_path, p, s+1, sizeof (imap_path));
+    mutt_buffer_concat_path (imap_path, p, s+1);
   }
   else
-    strfcpy (imap_path, s, sizeof(imap_path));
+    mutt_buffer_strcpy (imap_path, s);
 
-  if (mx_is_imap (imap_path))
-    return imap_complete (s, slen, imap_path);
+  if (mx_is_imap (mutt_b2s (imap_path)))
+  {
+    rc = imap_complete (s, slen, mutt_b2s (imap_path));
+    mutt_buffer_pool_release (&imap_path);
+    return rc;
+  }
+
+  mutt_buffer_pool_release (&imap_path);
 #endif
 
+  dirpart = mutt_buffer_pool_get ();
+  exp_dirpart = mutt_buffer_pool_get ();
+  filepart = mutt_buffer_pool_get ();
+  buf = mutt_buffer_pool_get ();
+
   if (*s == '=' || *s == '+' || *s == '!')
   {
-    dirpart[0] = *s;
-    dirpart[1] = 0;
+    mutt_buffer_addch (dirpart, *s);
     if (*s == '!')
-      strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart));
+      mutt_buffer_strcpy (exp_dirpart, NONULL (Spoolfile));
     else
-      strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart));
+      mutt_buffer_strcpy (exp_dirpart, NONULL (Maildir));
     if ((p = strrchr (s, '/')))
     {
-      char buf[_POSIX_PATH_MAX];
-      if (mutt_concatn_path (buf, sizeof(buf), exp_dirpart, strlen(exp_dirpart),
-                             s + 1, (size_t)(p - s - 1)) == NULL)
-      {
-        return -1;
-      }
-      strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
-      mutt_substrcpy(dirpart, s, p+1, sizeof(dirpart));
-      strfcpy (filepart, p + 1, sizeof (filepart));
+      mutt_buffer_concatn_path (buf,
+                                mutt_b2s (exp_dirpart), mutt_buffer_len (exp_dirpart),
+                                s + 1, (size_t)(p - s - 1));
+      mutt_buffer_strcpy (exp_dirpart, mutt_b2s (buf));
+      mutt_buffer_substrcpy (dirpart, s, p+1);
+      mutt_buffer_strcpy (filepart, p + 1);
     }
     else
-      strfcpy (filepart, s + 1, sizeof (filepart));
-    dirp = opendir (exp_dirpart);
+      mutt_buffer_strcpy (filepart, s + 1);
+    dirp = opendir (mutt_b2s (exp_dirpart));
   }
   else
   {
@@ -99,46 +111,45 @@ int mutt_complete (char *s, size_t slen)
       if (p == s) /* absolute path */
       {
        p = s + 1;
-       strfcpy (dirpart, "/", sizeof (dirpart));
-       exp_dirpart[0] = 0;
-       strfcpy (filepart, p, sizeof (filepart));
-       dirp = opendir (dirpart);
+       mutt_buffer_strcpy (dirpart, "/");
+       mutt_buffer_strcpy (filepart, p);
+       dirp = opendir (mutt_b2s (dirpart));
       }
       else
       {
-       mutt_substrcpy(dirpart, s, p, sizeof(dirpart));
-       strfcpy (filepart, p + 1, sizeof (filepart));
-       strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
-       mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
-       dirp = opendir (exp_dirpart);
+       mutt_buffer_substrcpy (dirpart, s, p);
+       mutt_buffer_strcpy (filepart, p + 1);
+       mutt_buffer_strcpy (exp_dirpart, mutt_b2s (dirpart));
+       mutt_buffer_expand_path (exp_dirpart);
+       dirp = opendir (mutt_b2s (exp_dirpart));
       }
     }
     else
     {
       /* no directory name, so assume current directory. */
-      dirpart[0] = 0;
-      strfcpy (filepart, s, sizeof (filepart));
+      mutt_buffer_strcpy (filepart, s);
       dirp = opendir (".");
     }
   }
 
   if (dirp == NULL)
   {
-    dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
-    return (-1);
+    dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n",
+                mutt_b2s (exp_dirpart), strerror (errno), errno));
+    goto cleanup;
   }
 
   /*
    * special case to handle when there is no filepart yet.  find the first
    * file/directory which is not ``.'' or ``..''
    */
-  if ((len = mutt_strlen (filepart)) == 0)
+  if ((len = mutt_buffer_len (filepart)) == 0)
   {
     while ((de = readdir (dirp)) != NULL)
     {
       if (mutt_strcmp (".", de->d_name) != 0 && mutt_strcmp ("..", de->d_name) != 0)
       {
-       strfcpy (filepart, de->d_name, sizeof (filepart));
+       mutt_buffer_strcpy (filepart, de->d_name);
        init++;
        break;
       }
@@ -147,54 +158,60 @@ int mutt_complete (char *s, size_t slen)
 
   while ((de = readdir (dirp)) != NULL)
   {
-    if (mutt_strncmp (de->d_name, filepart, len) == 0)
+    if (mutt_strncmp (de->d_name, mutt_b2s (filepart), len) == 0)
     {
       if (init)
       {
-       for (i=0; filepart[i] && de->d_name[i]; i++)
+        char *fpch;
+
+       for (i=0, fpch = filepart->data; *fpch && de->d_name[i]; i++, fpch++)
        {
-         if (filepart[i] != de->d_name[i])
-         {
-           filepart[i] = 0;
+         if (*fpch != de->d_name[i])
            break;
-         }
        }
-       filepart[i] = 0;
+        *fpch = 0;
+        mutt_buffer_fix_dptr (filepart);
       }
       else
       {
-       char buf[_POSIX_PATH_MAX];
        struct stat st;
 
-       strfcpy (filepart, de->d_name, sizeof(filepart));
+       mutt_buffer_strcpy (filepart, de->d_name);
 
        /* check to see if it is a directory */
-       if (dirpart[0])
+       if (mutt_buffer_len (dirpart))
        {
-         strfcpy (buf, exp_dirpart, sizeof (buf));
-         strfcpy (buf + strlen (buf), "/", sizeof (buf) - strlen (buf));
+         mutt_buffer_strcpy (buf, mutt_b2s (exp_dirpart));
+         mutt_buffer_addch (buf, '/');
        }
        else
-         buf[0] = 0;
-       strfcpy (buf + strlen (buf), filepart, sizeof (buf) - strlen (buf));
-       if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
-         strfcpy (filepart + strlen (filepart), "/",
-                  sizeof (filepart) - strlen (filepart));
+         mutt_buffer_clear (buf);
+       mutt_buffer_addstr (buf, mutt_b2s (filepart));
+       if (stat (mutt_b2s (buf), &st) != -1 && (st.st_mode & S_IFDIR))
+          mutt_buffer_addch (filepart, '/');
        init = 1;
       }
     }
   }
   closedir (dirp);
 
-  if (dirpart[0])
+  if (mutt_buffer_len (dirpart))
   {
-    strfcpy (s, dirpart, slen);
-    if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '=' && dirpart[0] != '+')
+    strfcpy (s, mutt_b2s (dirpart), slen);
+    if (mutt_strcmp ("/", mutt_b2s (dirpart)) != 0 &&
+        mutt_b2s (dirpart)[0] != '=' &&
+        mutt_b2s (dirpart)[0] != '+')
       strfcpy (s + strlen (s), "/", slen - strlen (s));
-    strfcpy (s + strlen (s), filepart, slen - strlen (s));
+    strfcpy (s + strlen (s), mutt_b2s (filepart), slen - strlen (s));
   }
   else
-    strfcpy (s, filepart, slen);
+    strfcpy (s, mutt_b2s (filepart), slen);
+
+cleanup:
+  mutt_buffer_pool_release (&dirpart);
+  mutt_buffer_pool_release (&exp_dirpart);
+  mutt_buffer_pool_release (&filepart);
+  mutt_buffer_pool_release (&buf);
 
   return (init ? 0 : -1);
 }
index e583a3286357f00e98b7d6260839edf6f48ccaba..ccba3a383525ce31db7119057e825af2a79ee65d 100644 (file)
@@ -2147,7 +2147,7 @@ imap_complete_hosts (char *dest, size_t len)
 
 /* imap_complete: given a partial IMAP folder path, return a string which
  *   adds as much to the path as is unique */
-int imap_complete(char* dest, size_t dlen, char* path)
+int imap_complete(char* dest, size_t dlen, const char* path)
 {
   IMAP_DATA* idata;
   char list[LONG_STRING];
index f85651951f578d2a08cc550beb19d9ae1f3f5a45..2a3d4fc0516dfafb85414fb019ec94fe0971fd02 100644 (file)
@@ -41,7 +41,7 @@ int imap_buffy_check (int force, int check_stats);
 int imap_status (const char *path, int queue);
 int imap_search (CONTEXT* ctx, const pattern_t* pat);
 int imap_subscribe (char *path, int subscribe);
-int imap_complete (char* dest, size_t dlen, char* path);
+int imap_complete (char* dest, size_t dlen, const char* path);
 int imap_fast_trash (CONTEXT* ctx, char* dest);
 
 void imap_allow_reopen (CONTEXT *ctx);
diff --git a/lib.c b/lib.c
index 16fa21a9c8b66cce85c266970df70c5fdddb1bbb..168de99a2f96c638d9cd4368a133290bbbf3e887 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -780,52 +780,6 @@ void mutt_remove_trailing_ws (char *s)
     *p = 0;
 }
 
-/*
- * Write the concatened pathname (dir + "/" + fname) into dst.
- * The slash is omitted when dir or fname is of 0 length.
- * Returns NULL on error or a pointer to dst otherwise.
- */
-char *mutt_concatn_path (char *dst, size_t dstlen,
-                         const char *dir, size_t dirlen, const char *fname, size_t fnamelen)
-{
-  size_t req;
-  size_t offset = 0;
-
-  if (dstlen == 0)
-    return NULL; /* probably should not mask errors like this */
-
-  /* size check */
-  req = dirlen + fnamelen + 1; /* +1 for the trailing nul */
-  if (dirlen && fnamelen)
-    req++; /* when both components are non-nul, we add a "/" in between */
-  if (req > dstlen) /* check for condition where the dst length is too short */
-  {
-    /* Two options here:
-     * 1) assert(0) or return NULL to signal error
-     * 2) copy as much of the path as will fit
-     * It doesn't appear that the return value is actually checked anywhere mutt_concat_path()
-     * is called, so we should just copy set dst to nul and let the calling function fail later.
-     */
-    dst[0] = 0; /* safe since we bail out early if dstlen == 0 */
-    return NULL;
-  }
-
-  if (dirlen) /* when dir is not empty */
-  {
-    memcpy(dst, dir, dirlen);
-    offset = dirlen;
-    if (fnamelen)
-      dst[offset++] = '/';
-  }
-  if (fnamelen) /* when fname is not empty */
-  {
-    memcpy(dst + offset, fname, fnamelen);
-    offset += fnamelen;
-  }
-  dst[offset] = 0;
-  return dst;
-}
-
 char *mutt_concat_path (char *d, const char *dir, const char *fname, size_t l)
 {
   const char *fmt = "%s/%s";
diff --git a/lib.h b/lib.h
index 09c25d76af6518020d6c52cef987383d9ac5b060..12fa85c453e09e2fc119333e686d86408cdf375c 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -169,7 +169,6 @@ void mutt_debug (FILE *, const char *, ...);
 
 /* The actual library functions. */
 
-char *mutt_concatn_path (char *, size_t, const char *, size_t, const char *, size_t);
 char *mutt_concat_path (char *, const char *, const char *, size_t);
 char *mutt_read_line (char *, size_t *, FILE *, int *, int);
 char *mutt_skip_whitespace (char *);
index 08458cfee6a1a99ae3627a22209542ce8b0705b7..78b4376948fb98c0f34f5f8c2e8b29edda08efae 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -1275,6 +1275,22 @@ void mutt_buffer_concat_path (BUFFER *d, const char *dir, const char *fname)
   mutt_buffer_printf (d, fmt, dir, fname);
 }
 
+/*
+ * Write the concatened pathname (dir + "/" + fname) into dst.
+ * The slash is omitted when dir or fname is of 0 length.
+ */
+void mutt_buffer_concatn_path (BUFFER *dst, const char *dir, size_t dirlen,
+                               const char *fname, size_t fnamelen)
+{
+  mutt_buffer_clear (dst);
+  if (dirlen)
+    mutt_buffer_addstr_n (dst, dir, dirlen);
+  if (dirlen && fnamelen)
+    mutt_buffer_addch (dst, '/');
+  if (fnamelen)
+    mutt_buffer_addstr_n (dst, fname, fnamelen);
+}
+
 const char *mutt_getcwd (BUFFER *cwd)
 {
   char *retval;
index 038d12c60f439a342dcbdb708b2e5f8c9026eb33..6b98d4411cc6721f9950eac6b6c750c7e1ae0387 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -175,6 +175,8 @@ int  mutt_bounce_message (FILE *fp, HEADER *, ADDRESS *);
 void mutt_break_thread (HEADER *);
 void mutt_browser_cleanup (void);
 void mutt_buffer_concat_path (BUFFER *, const char *, const char *);
+void mutt_buffer_concatn_path (BUFFER *dst, const char *dir, size_t dirlen,
+                               const char *fname, size_t fnamelen);
 #define mutt_buffer_quote_filename(a,b) _mutt_buffer_quote_filename (a, b, 1);
 void _mutt_buffer_quote_filename (BUFFER *, const char *, int);
 void mutt_buffer_sanitize_filename (BUFFER *d, const char *f, short slash);