]> granicus.if.org Git - neomutt/commitdiff
refactor: Split out BUFFER-handling functions (#443)
authorIan Zimmerman <nobrowser@users.noreply.github.com>
Tue, 7 Mar 2017 12:19:12 +0000 (04:19 -0800)
committerRichard Russon <rich@flatcap.org>
Tue, 7 Mar 2017 12:19:12 +0000 (12:19 +0000)
24 files changed:
Makefile.am
attach.c
buffer.c [new file with mode: 0644]
buffer.h [new file with mode: 0644]
commands.c
filter.c
filter.h [new file with mode: 0644]
gnupgparse.c
handler.c
init.c
lib.h
mbyte.c
mutt.h
muttlib.c
myvar.h [new file with mode: 0644]
parse.c
pgp.c
pgpinvoke.c
pgpkey.c
protos.h
query.c
recvattach.c
send.c
smime.c

index 6483156398f969a359217ecf7c7579009047cce9..37899e5ddbe348a7e0b115bc647a35043dad128c 100644 (file)
@@ -35,7 +35,7 @@ BUILT_SOURCES = keymap_defs.h patchlist.c git-ver.h conststrings.c hcachever.sh
 
 bin_PROGRAMS = mutt $(DOTLOCK_TARGET) $(PGPAUX_TARGET)
 mutt_SOURCES = \
-       addrbook.c alias.c attach.c base64.c browser.c buffy.c color.c \
+       addrbook.c alias.c attach.c base64.c browser.c buffer.c buffy.c color.c \
        crypt.c cryptglue.c \
        commands.c complete.c compose.c copy.c curs_lib.c curs_main.c date.c \
        edit.c enter.c flags.c init.c filter.c from.c \
@@ -75,8 +75,8 @@ EXTRA_mutt_SOURCES = account.c bcache.c compress.c crypt-gpgme.c crypt-mod-pgp-c
 
 EXTRA_DIST = COPYRIGHT LICENSE.md OPS OPS.PGP OPS.CRYPT OPS.SMIME TODO UPDATING \
        account.h \
-       attach.h buffy.h charset.h compress.h copy.h crypthash.h dotlock.h functions.h gen_defs \
-       globals.h hash.h history.h init.h keymap.h mutt_crypt.h \
+       attach.h buffer.h buffy.h charset.h compress.h copy.h crypthash.h dotlock.h functions.h gen_defs \
+       filter.h globals.h hash.h history.h init.h keymap.h mutt_crypt.h myvar.h \
        mailbox.h mapping.h md5.h mime.h mutt.h mutt_curses.h mutt_menu.h \
        mutt_regex.h mutt_sasl.h mutt_sasl_plain.h mutt_socket.h mutt_ssl.h \
        mutt_tunnel.h mx.h pager.h pgp.h pop.h protos.h rfc1524.h rfc2047.h \
index 9cb2f937bc3dc91bd865e1b475de41d49cda6b25..4bf455cf15fa1e31b5f474f1a36e64d6ec194fa1 100644 (file)
--- a/attach.c
+++ b/attach.c
@@ -23,6 +23,7 @@
 
 #include "mutt.h"
 #include "mutt_menu.h"
+#include "filter.h"
 #include "attach.h"
 #include "mutt_curses.h"
 #include "keymap.h"
diff --git a/buffer.c b/buffer.c
new file mode 100644 (file)
index 0000000..ab587b5
--- /dev/null
+++ b/buffer.c
@@ -0,0 +1,340 @@
+/*
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ * 
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lib.h"
+#include "myvar.h"
+#include "filter.h"
+#include "buffer.h"
+
+/* creates and initializes a BUFFER */
+BUFFER *mutt_buffer_new(void) {
+  BUFFER *b;
+
+  b = safe_malloc(sizeof(BUFFER));
+
+  mutt_buffer_init(b);
+
+  return b;
+}
+
+/* initialize a new BUFFER */
+BUFFER *mutt_buffer_init (BUFFER *b) {
+  memset(b, 0, sizeof(BUFFER));
+  return b;
+}
+
+/*
+ * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
+ * just initializes. Frees anything already in the buffer. Copies in
+ * the seed string.
+ *
+ * Disregards the 'destroy' flag, which seems reserved for caller.
+ * This is bad, but there's no apparent protocol for it.
+ */
+BUFFER *mutt_buffer_from (char *seed) {
+  BUFFER *b;
+
+  if (!seed)
+    return NULL;
+
+  b = mutt_buffer_new ();
+  b->data = safe_strdup(seed);
+  b->dsize = mutt_strlen(seed);
+  b->dptr = (char *) b->data + b->dsize;
+  return b;
+}
+
+void mutt_buffer_free (BUFFER **p)
+{
+  if (!p || !*p) 
+    return;
+
+   FREE(&(*p)->data);
+   /* dptr is just an offset to data and shouldn't be freed */
+   FREE(p);            /* __FREE_CHECKED__ */
+}
+
+int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
+{
+  va_list ap, ap_retry;
+  int len, blen, doff;
+  
+  va_start (ap, fmt);
+  va_copy (ap_retry, ap);
+
+  if (!buf->dptr)
+    buf->dptr = buf->data;
+
+  doff = buf->dptr - buf->data;
+  blen = buf->dsize - doff;
+  /* solaris 9 vsnprintf barfs when blen is 0 */
+  if (!blen)
+  {
+    blen = 128;
+    buf->dsize += blen;
+    safe_realloc (&buf->data, buf->dsize);
+    buf->dptr = buf->data + doff;
+  }
+  if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
+  {
+    blen = ++len - blen;
+    if (blen < 128)
+      blen = 128;
+    buf->dsize += blen;
+    safe_realloc (&buf->data, buf->dsize);
+    buf->dptr = buf->data + doff;
+    len = vsnprintf (buf->dptr, len, fmt, ap_retry);
+  }
+  if (len > 0)
+    buf->dptr += len;
+
+  va_end (ap);
+  va_end (ap_retry);
+
+  return len;
+}
+
+/* dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
+ * Always one byte bigger than necessary for the null terminator, and
+ * the buffer is always null-terminated */
+static void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
+{
+  size_t offset;
+
+  if (buf->dptr + len + 1 > buf->data + buf->dsize)
+  {
+    offset = buf->dptr - buf->data;
+    buf->dsize += len < 128 ? 128 : len + 1;
+    /* suppress compiler aliasing warning */
+    safe_realloc ((void**) (void*) &buf->data, buf->dsize);
+    buf->dptr = buf->data + offset;
+  }
+  memcpy (buf->dptr, s, len);
+  buf->dptr += len;
+  *(buf->dptr) = '\0';
+}
+
+void mutt_buffer_addstr (BUFFER* buf, const char* s)
+{
+  mutt_buffer_add (buf, s, mutt_strlen (s));
+}
+
+void mutt_buffer_addch (BUFFER* buf, char c)
+{
+  mutt_buffer_add (buf, &c, 1);
+}
+
+int mutt_extract_token (BUFFER *dest, BUFFER *tok, int flags)
+{
+  char         ch;
+  char         qc = 0; /* quote char */
+  char         *pc;
+
+  /* reset the destination pointer to the beginning of the buffer */
+  dest->dptr = dest->data;
+
+  SKIPWS (tok->dptr);
+  while ((ch = *tok->dptr))
+  {
+    if (!qc)
+    {
+      if ((ISSPACE (ch) && !(flags & MUTT_TOKEN_SPACE)) ||
+         (ch == '#' && !(flags & MUTT_TOKEN_COMMENT)) ||
+         (ch == '=' && (flags & MUTT_TOKEN_EQUAL)) ||
+         (ch == ';' && !(flags & MUTT_TOKEN_SEMICOLON)) ||
+         ((flags & MUTT_TOKEN_PATTERN) && strchr ("~%=!|", ch)))
+       break;
+    }
+
+    tok->dptr++;
+
+    if (ch == qc)
+      qc = 0; /* end of quote */
+    else if (!qc && (ch == '\'' || ch == '"') && !(flags & MUTT_TOKEN_QUOTE))
+      qc = ch;
+    else if (ch == '\\' && qc != '\'')
+    {
+       if (!*tok->dptr)
+           return -1; /* premature end of token */
+      switch (ch = *tok->dptr++)
+      {
+       case 'c':
+       case 'C':
+           if (!*tok->dptr)
+               return -1; /* premature end of token */
+         mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
+                                    - '@') & 0x7f);
+         tok->dptr++;
+         break;
+       case 'r':
+         mutt_buffer_addch (dest, '\r');
+         break;
+       case 'n':
+         mutt_buffer_addch (dest, '\n');
+         break;
+       case 't':
+         mutt_buffer_addch (dest, '\t');
+         break;
+       case 'f':
+         mutt_buffer_addch (dest, '\f');
+         break;
+       case 'e':
+         mutt_buffer_addch (dest, '\033');
+         break;
+       default:
+         if (isdigit ((unsigned char) ch) &&
+             isdigit ((unsigned char) *tok->dptr) &&
+             isdigit ((unsigned char) *(tok->dptr + 1)))
+         {
+
+           mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
+           tok->dptr += 2;
+         }
+         else
+           mutt_buffer_addch (dest, ch);
+      }
+    }
+    else if (ch == '^' && (flags & MUTT_TOKEN_CONDENSE))
+    {
+       if (!*tok->dptr)
+           return -1; /* premature end of token */
+      ch = *tok->dptr++;
+      if (ch == '^')
+       mutt_buffer_addch (dest, ch);
+      else if (ch == '[')
+       mutt_buffer_addch (dest, '\033');
+      else if (isalpha ((unsigned char) ch))
+       mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
+      else
+      {
+       mutt_buffer_addch (dest, '^');
+       mutt_buffer_addch (dest, ch);
+      }
+    }
+    else if (ch == '`' && (!qc || qc == '"'))
+    {
+      FILE     *fp;
+      pid_t    pid;
+      char     *cmd, *ptr;
+      size_t   expnlen;
+      BUFFER   expn;
+      int      line = 0;
+
+      pc = tok->dptr;
+      do {
+       if ((pc = strpbrk (pc, "\\`")))
+       {
+         /* skip any quoted chars */
+         if (*pc == '\\')
+           pc += 2;
+       }
+      } while (pc && *pc != '`');
+      if (!pc)
+      {
+       mutt_debug (1, "mutt_get_token: mismatched backticks\n");
+       return (-1);
+      }
+      cmd = mutt_substrdup (tok->dptr, pc);
+      if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
+      {
+       mutt_debug (1, "mutt_get_token: unable to fork command: %s", cmd);
+       FREE (&cmd);
+       return (-1);
+      }
+      FREE (&cmd);
+
+      tok->dptr = pc + 1;
+
+      /* read line */
+      mutt_buffer_init (&expn);
+      expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line, 0);
+      safe_fclose (&fp);
+      mutt_wait_filter (pid);
+
+      /* if we got output, make a new string consisting of the shell output
+        plus whatever else was left on the original line */
+      /* BUT: If this is inside a quoted string, directly add output to 
+       * the token */
+      if (expn.data && qc)
+      {
+       mutt_buffer_addstr (dest, expn.data);
+       FREE (&expn.data);
+      }
+      else if (expn.data)
+      {
+       expnlen = mutt_strlen (expn.data);
+       tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
+       ptr = safe_malloc (tok->dsize);
+       memcpy (ptr, expn.data, expnlen);
+       strcpy (ptr + expnlen, tok->dptr);      /* __STRCPY_CHECKED__ */
+       if (tok->destroy)
+         FREE (&tok->data);
+       tok->data = ptr;
+       tok->dptr = ptr;
+       tok->destroy = 1; /* mark that the caller should destroy this data */
+       ptr = NULL;
+       FREE (&expn.data);
+      }
+    }
+    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
+    {
+      const char *env = NULL;
+      char *var = NULL;
+      int idx;
+
+      if (*tok->dptr == '{')
+      {
+       tok->dptr++;
+       if ((pc = strchr (tok->dptr, '}')))
+       {
+         var = mutt_substrdup (tok->dptr, pc);
+         tok->dptr = pc + 1;
+       }
+      }
+      else
+      {
+       for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_'; pc++)
+         ;
+       var = mutt_substrdup (tok->dptr, pc);
+       tok->dptr = pc;
+      }
+      if (var)
+      {
+        if ((env = getenv (var)) || (env = myvar_get (var)))
+          mutt_buffer_addstr (dest, env);
+        else if ((idx = mutt_option_index (var)) != -1)
+        {
+          /* expand settable mutt variables */
+          char val[LONG_STRING];
+
+          if (var_to_string (idx, val, sizeof (val)))
+            mutt_buffer_addstr (dest, val);
+        }
+        FREE (&var);
+      }
+    }
+    else
+      mutt_buffer_addch (dest, ch);
+  }
+  mutt_buffer_addch (dest, 0); /* terminate the string */
+  SKIPWS (tok->dptr);
+  return 0;
+}
diff --git a/buffer.h b/buffer.h
new file mode 100644 (file)
index 0000000..a85aabe
--- /dev/null
+++ b/buffer.h
@@ -0,0 +1,49 @@
+/*
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ * 
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef BUFFER_INCLUDED
+#define BUFFER_INCLUDED
+
+#include <sys/types.h>
+
+typedef struct
+{
+  char *data;  /* pointer to data */
+  char *dptr;  /* current read/write position */
+  size_t dsize;        /* length of data */
+  int destroy; /* destroy `data' when done? */
+} BUFFER;
+
+/* flags for mutt_extract_token() */
+#define MUTT_TOKEN_EQUAL      1       /* treat '=' as a special */
+#define MUTT_TOKEN_CONDENSE   (1<<1)  /* ^(char) to control chars (macros) */
+#define MUTT_TOKEN_SPACE      (1<<2)  /* don't treat whitespace as a term */
+#define MUTT_TOKEN_QUOTE      (1<<3)  /* don't interpret quotes */
+#define MUTT_TOKEN_PATTERN    (1<<4)  /* !)|~ are terms (for patterns) */
+#define MUTT_TOKEN_COMMENT    (1<<5)  /* don't reap comments */
+#define MUTT_TOKEN_SEMICOLON  (1<<6)  /* don't treat ; as special */
+
+BUFFER *mutt_buffer_new (void);
+BUFFER * mutt_buffer_init (BUFFER *);
+BUFFER * mutt_buffer_from (char *);
+void mutt_buffer_free(BUFFER **);
+int mutt_buffer_printf (BUFFER*, const char*, ...);
+void mutt_buffer_addstr (BUFFER* buf, const char* s);
+void mutt_buffer_addch (BUFFER* buf, char c);
+int mutt_extract_token (BUFFER *, BUFFER *, int);
+
+#endif
+
index e119eeea4accdb4088e99c3099313e47d41f8173..e0fd8be85647f2283e37335a4f5f7beb70a16a54 100644 (file)
@@ -22,6 +22,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "mime.h"
index 0e94d249fa84f57e76ece2e8d5a565e5dafc6e1b..71a6b387e4dd6471dd535d3f10878d504fa674e6 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -22,6 +22,7 @@
 
 #include "mutt.h"
 #include "mutt_curses.h"
+#include "filter.h"
 
 #include <unistd.h>
 #include <stdlib.h>
diff --git a/filter.h b/filter.h
new file mode 100644 (file)
index 0000000..3ff902e
--- /dev/null
+++ b/filter.h
@@ -0,0 +1,30 @@
+/*
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ * 
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef FILTER_INCLUDED
+#define FILTER_INCLUDED
+
+#include <unistd.h>
+#include <stdio.h>
+
+pid_t
+mutt_create_filter_fd (const char *cmd, FILE **in, FILE **out, FILE **err,
+                       int fdin, int fdout, int fderr);
+pid_t mutt_create_filter (const char *s, FILE **in, FILE **out, FILE **err);
+int mutt_wait_filter (pid_t pid);
+
+#endif
+
index 6b608846fc77c1a2d607a98a16ca8ce7e1b6c127..424e4767aee37448dafcf0a4cc2fdcf4d1d1de11 100644 (file)
@@ -46,6 +46,7 @@
 #include <ctype.h>
 
 #include "mutt.h"
+#include "filter.h"
 #include "pgp.h"
 #include "charset.h"
 
index 2900ed841375fad47cef6101bfb4e056c34a79f4..08e03ee9048dcb2d04c427894947ae02627198e7 100644 (file)
--- a/handler.c
+++ b/handler.c
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "rfc1524.h"
 #include "keymap.h"
diff --git a/init.c b/init.c
index 66547ca5e4ed4a16969de4bb4efdf3dcffbd605d..e0aaf4b38c7e85cf0e2845cd48ee1502ac9c8a57 100644 (file)
--- a/init.c
+++ b/init.c
@@ -21,6 +21,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mapping.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
@@ -45,6 +46,7 @@
 #include "mx.h"
 #include "init.h"
 #include "mailbox.h"
+#include "myvar.h"
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -73,10 +75,7 @@ typedef struct myvar
 
 static myvar_t* MyVars;
 
-static int var_to_string (int idx, char* val, size_t len);
-
 static void myvar_set (const char* var, const char* val);
-static const char* myvar_get (const char* var);
 static void myvar_del (const char* var);
 
 #if USE_NOTMUCH
@@ -133,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.  */
-static int mutt_option_index (char *s)
+int mutt_option_index (char *s)
 {
   int i;
 
@@ -143,203 +142,6 @@ static int mutt_option_index (char *s)
   return (-1);
 }
 
-int mutt_extract_token (BUFFER *dest, BUFFER *tok, int flags)
-{
-  char         ch;
-  char         qc = 0; /* quote char */
-  char         *pc;
-
-  /* reset the destination pointer to the beginning of the buffer */
-  dest->dptr = dest->data;
-
-  SKIPWS (tok->dptr);
-  while ((ch = *tok->dptr))
-  {
-    if (!qc)
-    {
-      if ((ISSPACE (ch) && !(flags & MUTT_TOKEN_SPACE)) ||
-         (ch == '#' && !(flags & MUTT_TOKEN_COMMENT)) ||
-         (ch == '=' && (flags & MUTT_TOKEN_EQUAL)) ||
-         (ch == ';' && !(flags & MUTT_TOKEN_SEMICOLON)) ||
-         ((flags & MUTT_TOKEN_PATTERN) && strchr ("~%=!|", ch)))
-       break;
-    }
-
-    tok->dptr++;
-
-    if (ch == qc)
-      qc = 0; /* end of quote */
-    else if (!qc && (ch == '\'' || ch == '"') && !(flags & MUTT_TOKEN_QUOTE))
-      qc = ch;
-    else if (ch == '\\' && qc != '\'')
-    {
-       if (!*tok->dptr)
-           return -1; /* premature end of token */
-      switch (ch = *tok->dptr++)
-      {
-       case 'c':
-       case 'C':
-           if (!*tok->dptr)
-               return -1; /* premature end of token */
-         mutt_buffer_addch (dest, (toupper ((unsigned char) *tok->dptr)
-                                    - '@') & 0x7f);
-         tok->dptr++;
-         break;
-       case 'r':
-         mutt_buffer_addch (dest, '\r');
-         break;
-       case 'n':
-         mutt_buffer_addch (dest, '\n');
-         break;
-       case 't':
-         mutt_buffer_addch (dest, '\t');
-         break;
-       case 'f':
-         mutt_buffer_addch (dest, '\f');
-         break;
-       case 'e':
-         mutt_buffer_addch (dest, '\033');
-         break;
-       default:
-         if (isdigit ((unsigned char) ch) &&
-             isdigit ((unsigned char) *tok->dptr) &&
-             isdigit ((unsigned char) *(tok->dptr + 1)))
-         {
-
-           mutt_buffer_addch (dest, (ch << 6) + (*tok->dptr << 3) + *(tok->dptr + 1) - 3504);
-           tok->dptr += 2;
-         }
-         else
-           mutt_buffer_addch (dest, ch);
-      }
-    }
-    else if (ch == '^' && (flags & MUTT_TOKEN_CONDENSE))
-    {
-       if (!*tok->dptr)
-           return -1; /* premature end of token */
-      ch = *tok->dptr++;
-      if (ch == '^')
-       mutt_buffer_addch (dest, ch);
-      else if (ch == '[')
-       mutt_buffer_addch (dest, '\033');
-      else if (isalpha ((unsigned char) ch))
-       mutt_buffer_addch (dest, toupper ((unsigned char) ch) - '@');
-      else
-      {
-       mutt_buffer_addch (dest, '^');
-       mutt_buffer_addch (dest, ch);
-      }
-    }
-    else if (ch == '`' && (!qc || qc == '"'))
-    {
-      FILE     *fp;
-      pid_t    pid;
-      char     *cmd, *ptr;
-      size_t   expnlen;
-      BUFFER   expn;
-      int      line = 0;
-
-      pc = tok->dptr;
-      do {
-       if ((pc = strpbrk (pc, "\\`")))
-       {
-         /* skip any quoted chars */
-         if (*pc == '\\')
-           pc += 2;
-       }
-      } while (pc && *pc != '`');
-      if (!pc)
-      {
-        mutt_debug (1, "mutt_get_token: mismatched backticks\n");
-       return (-1);
-      }
-      cmd = mutt_substrdup (tok->dptr, pc);
-      if ((pid = mutt_create_filter (cmd, NULL, &fp, NULL)) < 0)
-      {
-        mutt_debug (1, "mutt_get_token: unable to fork command: %s", cmd);
-       FREE (&cmd);
-       return (-1);
-      }
-      FREE (&cmd);
-
-      tok->dptr = pc + 1;
-
-      /* read line */
-      mutt_buffer_init (&expn);
-      expn.data = mutt_read_line (NULL, &expn.dsize, fp, &line, 0);
-      safe_fclose (&fp);
-      mutt_wait_filter (pid);
-
-      /* if we got output, make a new string consisting of the shell output
-        plus whatever else was left on the original line */
-      /* BUT: If this is inside a quoted string, directly add output to 
-       * the token */
-      if (expn.data && qc)
-      {
-       mutt_buffer_addstr (dest, expn.data);
-       FREE (&expn.data);
-      }
-      else if (expn.data)
-      {
-       expnlen = mutt_strlen (expn.data);
-       tok->dsize = expnlen + mutt_strlen (tok->dptr) + 1;
-       ptr = safe_malloc (tok->dsize);
-       memcpy (ptr, expn.data, expnlen);
-       strcpy (ptr + expnlen, tok->dptr);      /* __STRCPY_CHECKED__ */
-       if (tok->destroy)
-         FREE (&tok->data);
-       tok->data = ptr;
-       tok->dptr = ptr;
-       tok->destroy = 1; /* mark that the caller should destroy this data */
-       ptr = NULL;
-       FREE (&expn.data);
-      }
-    }
-    else if (ch == '$' && (!qc || qc == '"') && (*tok->dptr == '{' || isalpha ((unsigned char) *tok->dptr)))
-    {
-      const char *env = NULL;
-      char *var = NULL;
-      int idx;
-
-      if (*tok->dptr == '{')
-      {
-       tok->dptr++;
-       if ((pc = strchr (tok->dptr, '}')))
-       {
-         var = mutt_substrdup (tok->dptr, pc);
-         tok->dptr = pc + 1;
-       }
-      }
-      else
-      {
-       for (pc = tok->dptr; isalnum ((unsigned char) *pc) || *pc == '_'; pc++)
-         ;
-       var = mutt_substrdup (tok->dptr, pc);
-       tok->dptr = pc;
-      }
-      if (var)
-      {
-        if ((env = getenv (var)) || (env = myvar_get (var)))
-          mutt_buffer_addstr (dest, env);
-        else if ((idx = mutt_option_index (var)) != -1)
-        {
-          /* expand settable mutt variables */
-          char val[LONG_STRING];
-
-          if (var_to_string (idx, val, sizeof (val)))
-            mutt_buffer_addstr (dest, val);
-        }
-        FREE (&var);
-      }
-    }
-    else
-      mutt_buffer_addch (dest, ch);
-  }
-  mutt_buffer_addch (dest, 0); /* terminate the string */
-  SKIPWS (tok->dptr);
-  return 0;
-}
-
 static void mutt_free_opt (struct option_t* p)
 {
   REGEXP* pp;
@@ -3368,7 +3170,7 @@ int mutt_nm_tag_complete (char *buffer, size_t len, int pos, int numtabs)
 }
 #endif
 
-static int var_to_string (int idx, char* val, size_t len)
+int var_to_string (int idx, char* val, size_t len)
 {
   char tmp[LONG_STRING];
   static const char * const vals[] = { "no", "yes", "ask-no", "ask-yes" };
@@ -4161,7 +3963,7 @@ static void myvar_del (const char* var)
   }
 }
 
-static const char* myvar_get (const char* var)
+const char* myvar_get (const char* var)
 {
   myvar_t* cur;
 
diff --git a/lib.h b/lib.h
index 10c379e4a62f254b043db33e726648a1d911e967..9266ad460a6ed0178c86bc1ec953de963890f7ae 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -37,6 +37,7 @@
 # include <stdarg.h>
 # include <stdbool.h>
 # include <signal.h>
+# include <ctype.h>
 
 # ifndef _POSIX_PATH_MAX
 #  include <limits.h>
diff --git a/mbyte.c b/mbyte.c
index faa3b60c2833f39547db8b1332500a25d5b3569a..097d8499a5fb8d7ab55bc1bcdbe71945c7d724bc 100644 (file)
--- a/mbyte.c
+++ b/mbyte.c
@@ -27,6 +27,7 @@
 #include "mutt.h"
 #include "mbyte.h"
 #include "charset.h"
+#include "buffer.h"
 
 #include <errno.h>
 
diff --git a/mutt.h b/mutt.h
index 7f54cfeadaf9fdc303fef0bb524ff54a75d9f830..5c364a53f1e16d1a2af14f2729578fa004f795a7 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -97,6 +97,7 @@
 
 #define WHERE_DEFINED 1
 
+#include "buffer.h"
 #include "mutt_regex.h"
 
 /* flags for mutt_enter_string() */
 #define  MUTT_NM_TAG   (1<<10) /* Notmuch tag +/- mode. */
 #endif
 
-/* flags for mutt_get_token() */
-#define MUTT_TOKEN_EQUAL      1       /* treat '=' as a special */
-#define MUTT_TOKEN_CONDENSE   (1<<1)  /* ^(char) to control chars (macros) */
-#define MUTT_TOKEN_SPACE      (1<<2)  /* don't treat whitespace as a term */
-#define MUTT_TOKEN_QUOTE      (1<<3)  /* don't interpret quotes */
-#define MUTT_TOKEN_PATTERN    (1<<4)  /* !)|~ are terms (for patterns) */
-#define MUTT_TOKEN_COMMENT    (1<<5)  /* don't reap comments */
-#define MUTT_TOKEN_SEMICOLON  (1<<6)  /* don't treat ; as special */
-
-typedef struct
-{
-  char *data;  /* pointer to data */
-  char *dptr;  /* current read/write position */
-  size_t dsize;        /* length of data */
-  int destroy; /* destroy `data' when done? */
-} BUFFER;
-
 /* flags for _mutt_system() */
 #define MUTT_DETACH_PROCESS    1       /* detach subprocess from group */
 
index 3e9d0f264ce55785c454de41e40579d1bad9824c..afff1373d35382413c4ebfc54bb762ee23fd4936 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -21,7 +21,9 @@
 # include "config.h"
 #endif
 
+#include "buffer.h"
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "mime.h"
 #include "mailbox.h"
@@ -1978,124 +1980,6 @@ void mutt_sleep (short s)
     sleep(s);
 }
 
-/* creates and initializes a BUFFER */
-BUFFER *mutt_buffer_new(void) {
-  BUFFER *b;
-
-  b = safe_malloc(sizeof(BUFFER));
-
-  mutt_buffer_init(b);
-
-  return b;
-}
-
-/* initialize a new BUFFER */
-BUFFER *mutt_buffer_init (BUFFER *b) {
-  memset(b, 0, sizeof(BUFFER));
-  return b;
-}
-
-/*
- * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
- * just initializes. Frees anything already in the buffer. Copies in
- * the seed string.
- *
- * Disregards the 'destroy' flag, which seems reserved for caller.
- * This is bad, but there's no apparent protocol for it.
- */
-BUFFER *mutt_buffer_from (char *seed) {
-  BUFFER *b;
-
-  if (!seed)
-    return NULL;
-
-  b = mutt_buffer_new ();
-  b->data = safe_strdup(seed);
-  b->dsize = mutt_strlen(seed);
-  b->dptr = (char *) b->data + b->dsize;
-  return b;
-}
-
-int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
-{
-  va_list ap, ap_retry;
-  int len, blen, doff;
-  
-  va_start (ap, fmt);
-  va_copy (ap_retry, ap);
-
-  if (!buf->dptr)
-    buf->dptr = buf->data;
-
-  doff = buf->dptr - buf->data;
-  blen = buf->dsize - doff;
-  /* solaris 9 vsnprintf barfs when blen is 0 */
-  if (!blen)
-  {
-    blen = 128;
-    buf->dsize += blen;
-    safe_realloc (&buf->data, buf->dsize);
-    buf->dptr = buf->data + doff;
-  }
-  if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
-  {
-    blen = ++len - blen;
-    if (blen < 128)
-      blen = 128;
-    buf->dsize += blen;
-    safe_realloc (&buf->data, buf->dsize);
-    buf->dptr = buf->data + doff;
-    len = vsnprintf (buf->dptr, len, fmt, ap_retry);
-  }
-  if (len > 0)
-    buf->dptr += len;
-
-  va_end (ap);
-  va_end (ap_retry);
-
-  return len;
-}
-
-void mutt_buffer_addstr (BUFFER* buf, const char* s)
-{
-  mutt_buffer_add (buf, s, mutt_strlen (s));
-}
-
-void mutt_buffer_addch (BUFFER* buf, char c)
-{
-  mutt_buffer_add (buf, &c, 1);
-}
-
-void mutt_buffer_free (BUFFER **p)
-{
-  if (!p || !*p) 
-    return;
-
-   FREE(&(*p)->data);
-   /* dptr is just an offset to data and shouldn't be freed */
-   FREE(p);            /* __FREE_CHECKED__ */
-}
-
-/* dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
- * Always one byte bigger than necessary for the null terminator, and
- * the buffer is always null-terminated */
-void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
-{
-  size_t offset;
-
-  if (buf->dptr + len + 1 > buf->data + buf->dsize)
-  {
-    offset = buf->dptr - buf->data;
-    buf->dsize += len < 128 ? 128 : len + 1;
-    /* suppress compiler aliasing warning */
-    safe_realloc ((void**) (void*) &buf->data, buf->dsize);
-    buf->dptr = buf->data + offset;
-  }
-  memcpy (buf->dptr, s, len);
-  buf->dptr += len;
-  *(buf->dptr) = '\0';
-}
-
 /* Decrease a file's modification time by 1 second */
 
 time_t mutt_decrease_mtime (const char *f, struct stat *st)
diff --git a/myvar.h b/myvar.h
new file mode 100644 (file)
index 0000000..b713713
--- /dev/null
+++ b/myvar.h
@@ -0,0 +1,27 @@
+/*
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ * 
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef MYVAR_INCLUDED
+#define MYVAR_INCLUDED
+
+#include <sys/types.h>
+
+const char* myvar_get (const char* var);
+int var_to_string (int idx, char* val, size_t len);
+int mutt_option_index (char *s);
+
+#endif
+
diff --git a/parse.c b/parse.c
index da006af9d090e3ed22cde8f8f531549c2f9c0afe..5b0414307e0db8291a57ab2ef59a015d92c557ba 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -28,6 +28,7 @@
 #include "rfc2231.h"
 #include "mutt_crypt.h"
 #include "url.h"
+#include "buffer.h"
 
 #include <string.h>
 #include <ctype.h>
diff --git a/pgp.c b/pgp.c
index b2ff4dc50df090db37255ea8fa8ef385e4361432..ebfd1779086a79e600505ce4ab715db316fd7045 100644 (file)
--- a/pgp.c
+++ b/pgp.c
@@ -31,6 +31,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_crypt.h"
 #include "mutt_curses.h"
 #include "pgp.h"
index 4df42112d4cd4b8a0b5434d6dd40481936d809f1..4ceed636f9fdf429b54f88416da2b5ff29d1aa12 100644 (file)
@@ -38,6 +38,7 @@
 #include <time.h>
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "mutt_idna.h"
 #include "pgp.h"
index ee58959a4504134621aade4103734985cff1c9b9..cf56bd4ff2c33f4441814346d793a5d8d9cc263d 100644 (file)
--- a/pgpkey.c
+++ b/pgpkey.c
@@ -25,6 +25,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "mime.h"
index 6e2e9b969a6e5a8ae71a9cea17daa6bea2e69f48..1ac1e4c887e767470ea6dee598d23a926ab02bcf 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -44,12 +44,6 @@ typedef enum {
 
 void mutt_make_string_info (char *, size_t, int, const char *, struct hdr_format_info *, format_flag);
 
-int mutt_extract_token (BUFFER *, BUFFER *, int);
-BUFFER *mutt_buffer_new (void);
-BUFFER * mutt_buffer_init (BUFFER *);
-BUFFER * mutt_buffer_from (char *);
-void mutt_buffer_free(BUFFER **);
-int mutt_buffer_printf (BUFFER*, const char*, ...);
 void mutt_buffer_add (BUFFER*, const char*, size_t);
 void mutt_buffer_addstr (BUFFER*, const char*);
 void mutt_buffer_addch (BUFFER*, char);
@@ -388,7 +382,6 @@ int mutt_compose_menu (HEADER *, char *, size_t, HEADER *, int);
 int mutt_thread_set_flag (HEADER *, int, int, int);
 int mutt_user_is_recipient (HEADER *);
 void mutt_update_num_postponed (void);
-int mutt_wait_filter (pid_t);
 int mutt_which_case (const char *);
 int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int, char *, char **);
 int mutt_write_multiple_fcc (const char *path, HEADER *hdr, const char *msgid, int, char *, char **);
@@ -413,9 +406,6 @@ uint64_t mutt_rand64(void);
 
 int mh_valid_message (const char *);
 
-pid_t mutt_create_filter (const char *, FILE **, FILE **, FILE **);
-pid_t mutt_create_filter_fd (const char *, FILE **, FILE **, FILE **, int, int, int);
-
 ADDRESS *alias_reverse_lookup (ADDRESS *);
 
 /* base64.c */
diff --git a/query.c b/query.c
index a06b24ac7f6bc1fcff2de2d8198ad83544cd44cf..ad61b95cdc95c3c68894f0b04b209dc9e2045939 100644 (file)
--- a/query.c
+++ b/query.c
@@ -21,6 +21,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_menu.h"
 #include "mutt_idna.h"
 #include "mapping.h"
index 3e5c1fb5e29b78ceb8b5bd19a29051a6bb712b0d..66d54c88c78394e9a10545f588bcff9d99cb5208 100644 (file)
@@ -22,6 +22,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "rfc1524.h"
diff --git a/send.c b/send.c
index c09d16b1d29ac5f84efa81a58c71c75b0822e38d..237a2138ee43050760b0293edff83e2ac0f9f0f3 100644 (file)
--- a/send.c
+++ b/send.c
@@ -21,6 +21,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "rfc2047.h"
 #include "keymap.h"
diff --git a/smime.c b/smime.c
index 84a53d78fc69995bd2daf0991a7fae2b743a871a..6fa968811b5a80389e9fa3ad5ab5c9ac660ea80b 100644 (file)
--- a/smime.c
+++ b/smime.c
@@ -23,6 +23,7 @@
 #endif
 
 #include "mutt.h"
+#include "filter.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "smime.h"