fully rework mutt_FormatString, fix a lot of issues, no more stack
[apps/madmutt.git] / muttlib.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 #include <lib-lib/lib-lib.h>
12
13 #include <grp.h>
14 #include <pwd.h>
15
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/enter.h>
19 #include <lib-sys/unix.h>
20 #include <lib-mx/mx.h>
21
22 #include "alias.h"
23 #include "mutt.h"
24 #include "attach.h"
25
26 #include "version.h"
27
28 #include <imap/imap.h>
29
30 #include <lib-crypt/crypt.h>
31
32 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
33
34 /* Modified by blong to accept a "suggestion" for file name.  If
35  * that file exists, then construct one with unique name but 
36  * keep any extension.  This might fail, I guess.
37  * Renamed to mutt_adv_mktemp so I only have to change where it's
38  * called, and not all possible cases.
39  */
40 void mutt_adv_mktemp (const char* dir, char *s, ssize_t l)
41 {
42     int fd;
43
44     fd = m_tempfd(s, l, m_strisempty(dir) ? NONULL(Tempdir) : dir, s);
45     if (fd < 0) {
46         *s = '\0';
47     } else {
48         close(fd);
49         unlink(s);
50     }
51 }
52
53 /* returns true if the header contained in "s" is in list "t" */
54 int mutt_matches_ignore (const char *s, string_list_t * t)
55 {
56   for (; t; t = t->next) {
57     if (!ascii_strncasecmp (s, t->data, m_strlen(t->data))
58         || *t->data == '*')
59       return 1;
60   }
61   return 0;
62 }
63
64 ssize_t _mutt_expand_path(char *s, ssize_t slen, int rx)
65 {
66     char p[_POSIX_PATH_MAX] = "";
67     char tmp[_POSIX_PATH_MAX];
68     const char *tail = "";
69
70     do {
71         const address_t *alias;
72
73         switch (*s) {
74           case '~':
75             if (s[1] == '/' || s[1] == '\0') {
76                 m_strcpy(p, sizeof(p), Homedir);
77                 tail = s + 1;
78             } else {
79                 struct passwd *pw;
80                 tail = m_strchrnul(s + 1, '/');
81
82                 m_strncpy(tmp, sizeof(tmp), s + 1, tail - s - 1);
83
84                 if ((pw = getpwnam(tmp))) {
85                     m_strcpy(p, sizeof(p), pw->pw_dir);
86                 } else {
87                     /* user not found! */
88                     tail = s;
89                 }
90             }
91             break;
92
93           case '=':
94           case '+':
95             /* if folder = imap[s]://host/: don't append slash */
96             if (imap_is_magic(NONULL(Maildir), NULL) == M_IMAP
97             &&  Maildir[m_strlen(Maildir) - 1] == '/') {
98                 m_strcpy(p, sizeof(p), Maildir);
99             } else {
100                 snprintf(p, sizeof(p), "%s/", NONULL(Maildir));
101             }
102
103             tail = s + 1;
104             break;
105
106             /* elm compatibility, @ expands alias to user name */
107
108           case '@':
109             if ((alias = alias_lookup(s + 1))) {
110                 HEADER h;
111                 header_init(&h);
112                 h.env = envelope_new();
113                 h.env->from = h.env->to = (address_t *)alias;
114                 mutt_default_save(p, sizeof (p), &h);
115                 h.env->from = h.env->to = NULL;
116                 header_wipe(&h);
117
118                 if (*p != '@') {
119                     /* recurse iff the result do not starts with '@' */
120                     m_strcpy(s, slen, p);
121                     continue;
122                 }
123             }
124             break;
125
126           case '>':
127             m_strcpy(p, sizeof(p), Inbox);
128             tail = s + 1;
129             break;
130
131           case '<':
132             m_strcpy(p, sizeof(p), Outbox);
133             tail = s + 1;
134             break;
135
136           case '!':
137             if (s[1] == '!') {
138                 m_strcpy(p, sizeof(p), LastFolder);
139                 tail = s + 2;
140             } else {
141                 m_strcpy(p, sizeof(p), Spoolfile);
142                 tail = s + 1;
143             }
144             break;
145
146           case '-':
147             m_strcpy(p, sizeof(p), NONULL(LastFolder));
148             tail = s + 1;
149             break;
150
151           case '^':
152             m_strcpy(p, sizeof(p), NONULL(CurrentFolder));
153             tail = s + 1;
154             break;
155
156           default:
157             *p = '\0';
158             tail = s;
159         }
160     } while (0);
161
162     if (rx) {
163         char q[_POSIX_PATH_MAX];
164         rx_sanitize_string(q, sizeof(q), p);
165         snprintf(tmp, sizeof(tmp), "%s%s", q, tail);
166     } else {
167         snprintf(tmp, sizeof(tmp), "%s%s", p, tail);
168     }
169
170     return m_strcpy(s, slen, tmp);
171 }
172
173 void mutt_mktemp(char *s)
174 {
175     int fd = m_tempfd(s, _POSIX_PATH_MAX, NONULL(Tempdir), NULL);
176     if (fd < 0) {
177         *s = '\0';
178     } else {
179         close(fd);
180         unlink(s);
181     }
182 }
183
184 /* collapse the pathname using ~ or = when possible */
185 void mutt_pretty_mailbox (char *s)
186 {
187   char *p = s, *q = s;
188   ssize_t len;
189   url_scheme_t scheme;
190
191   scheme = url_check_scheme (s);
192
193   if (scheme == U_IMAP || scheme == U_IMAPS) {
194     imap_pretty_mailbox (s);
195     return;
196   }
197
198   /* if s is an url, only collapse path component */
199   if (scheme != U_UNKNOWN) {
200     p = strchr (s, ':') + 1;
201     if (!m_strncmp (p, "//", 2))
202       q = strchr (p + 2, '/');
203     if (!q)
204       q = strchr (p, '\0');
205     p = q;
206   }
207
208   /* first attempt to collapse the pathname */
209   while (*p) {
210     if (*p == '/' && p[1] == '/') {
211       *q++ = '/';
212       p += 2;
213     }
214     else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
215       *q++ = '/';
216       p += 3;
217     }
218     else
219       *q++ = *p++;
220   }
221   *q = 0;
222
223   if (m_strncmp(s, Maildir, (len = m_strlen(Maildir))) == 0 &&
224       s[len] == '/') {
225     *s++ = '=';
226     memmove (s, s + len, m_strlen(s + len) + 1);
227   }
228   else if (m_strncmp(s, Homedir, (len = m_strlen(Homedir))) == 0 &&
229            s[len] == '/') {
230     *s++ = '~';
231     memmove (s, s + len - 1, m_strlen(s + len - 1) + 1);
232   }
233 }
234
235 /* return 0 on success, -1 on abort, 1 on error */
236 int mutt_check_overwrite (const char *attname, const char *path,
237                           char *fname, ssize_t flen, int *append,
238                           char **directory)
239 {
240   int rc = 0;
241   char tmp[_POSIX_PATH_MAX];
242   struct stat st;
243
244   m_strcpy(fname, flen, path);
245   if (access (fname, F_OK) != 0)
246     return 0;
247   if (stat (fname, &st) != 0)
248     return -1;
249   if (S_ISDIR (st.st_mode)) {
250     if (directory) {
251       switch (mutt_multi_choice
252               (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
253                _("yna"))) {
254       case 3:                  /* all */
255         m_strreplace(directory, fname);
256         break;
257       case 1:                  /* yes */
258         p_delete(directory);
259         break;
260       case -1:                 /* abort */
261         p_delete(directory);
262         return -1;
263       case 2:                  /* no */
264         p_delete(directory);
265         return 1;
266       }
267     }
268     else
269       if ((rc = mutt_yesorno(_("File is a directory, save under it?"),
270                              M_YES)) != M_YES)
271       return (rc == M_NO) ? 1 : -1;
272
273     if (!attname || !attname[0]) {
274       tmp[0] = 0;
275       if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
276                           M_FILE | M_CLEAR) != 0 || !tmp[0])
277         return (-1);
278       mutt_concat_path(fname, flen, path, tmp);
279     }
280     else
281       mutt_concat_path(fname, flen, path, mutt_basename(attname));
282   }
283
284   if (*append == 0 && access (fname, F_OK) == 0) {
285     switch (mutt_multi_choice
286             (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
287     {
288     case -1:                   /* abort */
289       return -1;
290     case 3:                    /* cancel */
291       return 1;
292
293     case 2:                    /* append */
294       *append = M_SAVE_APPEND;
295       break;
296     case 1:                    /* overwrite */
297       *append = M_SAVE_OVERWRITE;
298       break;
299     }
300   }
301   return 0;
302 }
303
304 void mutt_save_path(char *d, ssize_t dsize, address_t *a)
305 {
306     if (a && a->mailbox) {
307         m_strcpy(d, dsize, a->mailbox);
308
309         if (!option(OPTSAVEADDRESS)) {
310             char *p = strpbrk(d, "%@");
311             if (p)
312                 *p = '\0';
313         }
314         m_strtolower(d);
315     } else {
316         *d = '\0';
317     }
318 }
319
320 void mutt_safe_path(char *s, ssize_t l, address_t *a)
321 {
322     mutt_save_path(s, l, a);
323
324     while (*s) {
325         if (*s == '/' || ISSPACE(*s) || !isprint((unsigned char)*s))
326             *s = '_';
327         s++;
328     }
329 }
330
331 ssize_t
332 mutt_FormatString(char *dst, ssize_t dlen, const char *fmt,
333                   format_t *callback, unsigned long data, format_flag flags)
334 {
335     ssize_t pos = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
336
337     while (*fmt) {
338         char ifstring[STRING] = "", elsestring[STRING] = "", prefix[STRING] = "";
339         int ch;
340
341         if (*fmt == '%') {
342             if (*++fmt == '%') {
343                 pos += m_strputc(dst + pos, dlen - pos, '%');
344                 fmt++;
345                 continue;
346             }
347
348             if (*fmt == '?') {
349                 flags |= M_FORMAT_OPTIONAL;
350                 fmt++;
351             } else {
352                 ssize_t pfxlen;
353                 flags &= ~M_FORMAT_OPTIONAL;
354
355                 /* eat the format string */
356                 pfxlen = strspn(fmt, "0123456789.-");
357                 m_strncpy(prefix, sizeof(prefix), fmt, pfxlen);
358                 fmt   += pfxlen;
359             }
360
361             /* save the character to switch on */
362             if (!(ch = *fmt++))
363                 break;
364
365             if (flags & M_FORMAT_OPTIONAL) {
366                 ssize_t iflen;
367                 const char *p;
368
369                 if (*fmt++ != '?')
370                     break;                /* bad format */
371
372                 /* eat the `if' part of the string */
373                 iflen = strcspn(fmt, "?&");
374                 m_strncpy(ifstring, ssizeof(ifstring), fmt, iflen);
375                 fmt  += iflen;
376
377                 /* eat the `else' part of the string (optional) */
378                 if (*fmt == '&')
379                     fmt++;                /* skip the & */
380
381                 p = m_strchrnul(fmt, '?');
382                 m_strncpy(elsestring, ssizeof(elsestring), fmt, p - fmt);
383                 fmt = p;
384
385                 if (!*fmt++)              /* move past the trailing `?' */
386                     break;                /* bad format */
387             }
388
389             switch (ch) {
390                 ssize_t col;
391                 char lower, nodots, buf[LONG_STRING];
392
393               case '>':                 /* right justify to EOL */
394                 col = mutt_strwidth(dst);
395
396                 ch = *fmt++;            /* pad char */
397
398                 if (COLS - SW > col) {
399                     ssize_t wid;
400
401                     mutt_FormatString(buf, sizeof(buf), fmt, callback, data, flags);
402                     wid = mutt_strwidth(buf);
403
404                     pos += m_strpad(dst + pos, dlen - pos, ch, COLS - SW - col - wid);
405                     pos += m_strcpy(dst + pos, dlen - pos, buf);
406                 }
407                 return pos;             /* skip rest of input */
408
409               case '|':
410                 col = mutt_strwidth(dst);
411
412                 ch = *fmt++;
413                 /* pad to EOL */
414                 pos += m_strpad(dst + pos, dlen - pos, ch, COLS - SW - col);
415                 return pos;             /* skip rest of input */
416
417               default:
418                 lower = nodots = 0;
419
420                 while (ch == '_' || ch == ':') {
421                     lower  |= ch == '_';
422                     nodots |= ch == ':';
423                     ch = *fmt++;
424                 }
425
426                 /* use callback function to handle this case */
427                 fmt = callback(buf, sizeof (buf), ch, fmt, prefix,
428                                ifstring, elsestring, data, flags);
429
430                 if (lower)
431                     m_strtolower(buf);
432
433                 if (nodots) {
434                     char *p;
435
436                     for (p = buf; *p; p++) {
437                         if (*p == '.')
438                             *p = '_';
439                     }
440                 }
441
442                 pos += m_strcpy(dst + pos, dlen - pos, buf);
443                 continue;
444             }
445         }
446
447         if (*fmt == '\\') {
448             if (!*++fmt)
449                 break;
450             switch (*fmt) {
451               case 'n': pos += m_strputc(dst + pos, dlen - pos, '\n'); break;
452               case 't': pos += m_strputc(dst + pos, dlen - pos, '\t'); break;
453               case 'r': pos += m_strputc(dst + pos, dlen - pos, '\r'); break;
454               case 'f': pos += m_strputc(dst + pos, dlen - pos, '\f'); break;
455               case 'v': pos += m_strputc(dst + pos, dlen - pos, '\v'); break;
456               default:  pos += m_strputc(dst + pos, dlen - pos, *fmt); break;
457             }
458             fmt++;
459         } else {
460             ssize_t len = strcspn(fmt, "%\\");
461
462             pos += m_strncpy(dst + pos, dlen - pos, fmt, len);
463             fmt += len;
464         }
465     }
466
467     return pos;
468 }
469
470 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
471 int mutt_save_confirm (const char *s, struct stat *st)
472 {
473   char tmp[_POSIX_PATH_MAX];
474   int ret = 0;
475   int rc;
476   int magic = 0;
477
478   magic = mx_get_magic (s);
479
480   if (magic == M_POP) {
481     mutt_error _("Can't save message to POP mailbox.");
482
483     return 1;
484   }
485
486 #ifdef USE_NNTP
487   if (magic == M_NNTP) {
488     mutt_error _("Can't save message to newsserver.");
489
490     return 0;
491   }
492 #endif
493
494   if (magic > 0 && !mx_access (s, W_OK)) {
495     if (option (OPTCONFIRMAPPEND) &&
496         (!TrashPath || (m_strcmp(s, TrashPath) != 0))) {
497       /* if we're appending to the trash, there's no point in asking */
498       snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
499       if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
500         ret = 1;
501       else if (rc == -1)
502         ret = -1;
503     }
504   }
505
506   if (stat (s, st) != -1) {
507     if (magic == -1) {
508       mutt_error (_("%s is not a mailbox!"), s);
509       return 1;
510     }
511   } else {
512     if (magic != M_IMAP)
513     {
514       st->st_mtime = 0;
515       st->st_atime = 0;
516
517       if (errno == ENOENT) {
518         if (option (OPTCONFIRMCREATE)) {
519           snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
520           if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
521             ret = 1;
522           else if (rc == -1)
523             ret = -1;
524         }
525       } else {
526         mutt_perror (s);
527         return 1;
528       }
529     }
530   }
531
532   CLEARLINE (LINES - 1);
533   return (ret);
534 }
535
536 void mutt_sleep (short s)
537 {
538     sleep(MAX(s, SleepTime));
539 }
540
541 const char *mutt_make_version (int full)
542 {
543   static char vstring[STRING];
544
545   if (full)
546     snprintf (vstring, sizeof (vstring),
547               "Madmutt/%s-r%s (based on Mutt 1.5.11)",
548               MUTT_VERSION, MUTT_REVISION);
549   else
550     snprintf (vstring, sizeof (vstring), "Madmutt/%s-%s",
551               MUTT_VERSION, MUTT_REVISION);
552   return vstring;
553 }
554
555 /* return 1 if address lists are strictly identical */
556 static int mutt_cmp_addr (const address_t * a, const address_t * b)
557 {
558   while (a && b) {
559     if (m_strcmp(a->mailbox, b->mailbox) ||
560         m_strcmp(a->personal, b->personal))
561       return (0);
562
563     a = a->next;
564     b = b->next;
565   }
566   if (a || b)
567     return (0);
568
569   return (1);
570 }
571
572 static int mutt_cmp_list (const string_list_t * a, const string_list_t * b)
573 {
574   while (a && b) {
575     if (m_strcmp(a->data, b->data))
576       return (0);
577
578     a = a->next;
579     b = b->next;
580   }
581   if (a || b)
582     return (0);
583
584   return (1);
585 }
586
587 static int mutt_cmp_env (const ENVELOPE * e1, const ENVELOPE * e2)
588 {
589   if (e1 && e2) {
590     if (m_strcmp(e1->message_id, e2->message_id) ||
591         m_strcmp(e1->subject, e2->subject) ||
592         !mutt_cmp_list (e1->references, e2->references) ||
593         !mutt_cmp_addr (e1->from, e2->from) ||
594         !mutt_cmp_addr (e1->sender, e2->sender) ||
595         !mutt_cmp_addr (e1->reply_to, e2->reply_to) ||
596         !mutt_cmp_addr (e1->to, e2->to) ||
597         !mutt_cmp_addr (e1->cc, e2->cc) ||
598         !mutt_cmp_addr (e1->return_path, e2->return_path))
599       return (0);
600     else
601       return (1);
602   }
603   else {
604     if (e1 == NULL && e2 == NULL)
605       return (1);
606     else
607       return (0);
608   }
609 }
610
611 static int mutt_cmp_body (const BODY * b1, const BODY * b2)
612 {
613   if (b1->type != b2->type ||
614       b1->encoding != b2->encoding ||
615       m_strcmp(b1->subtype, b2->subtype) ||
616       m_strcmp(b1->description, b2->description) ||
617       !parameter_equal(b1->parameter, b2->parameter) ||
618       b1->length != b2->length)
619     return (0);
620   return (1);
621 }
622 int mutt_cmp_header (const HEADER * h1, const HEADER * h2) {
623   if (h1 && h2) {
624     if (h1->received != h2->received ||
625         h1->date_sent != h2->date_sent ||
626         h1->content->length != h2->content->length ||
627         h1->lines != h2->lines ||
628         h1->zhours != h2->zhours ||
629         h1->zminutes != h2->zminutes ||
630         h1->zoccident != h2->zoccident ||
631         h1->mime != h2->mime ||
632         !mutt_cmp_env (h1->env, h2->env) ||
633         !mutt_cmp_body (h1->content, h2->content))
634       return (0);
635     else
636       return (1);
637   }
638   else {
639     if (h1 == NULL && h2 == NULL)
640       return (1);
641     else
642       return (0);
643   }
644 }
645
646
647 int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags)
648 {
649     char ch;
650     char qc = 0;                  /* quote char */
651     char *pc;
652
653     /* reset the destination pointer to the beginning of the buffer */
654     dest->dptr = dest->data;
655
656     tok->dptr = vskipspaces(tok->dptr);
657     while ((ch = *tok->dptr)) {
658         if (!qc) {
659             if ((ISSPACE(ch) && !(flags & M_TOKEN_SPACE))
660             || (ch == '#' && !(flags & M_TOKEN_COMMENT))
661             || (ch == '=' && (flags & M_TOKEN_EQUAL))
662             || (ch == ';' && !(flags & M_TOKEN_SEMICOLON))
663             || ((flags & M_TOKEN_PATTERN) && strchr("~=!|", ch)))
664             {
665                 break;
666             }
667         }
668
669         tok->dptr++;
670
671         if (ch == qc) {
672             qc = 0;                     /* end of quote */
673         } else
674         if (!qc && (ch == '\'' || ch == '"') && !(flags & M_TOKEN_QUOTE)) {
675             qc = ch;
676         } else
677         if (ch == '\\' && qc != '\'') {
678             if (!*tok->dptr)
679                 return -1;              /* premature end of token */
680
681             switch (ch = *tok->dptr++) {
682               case 'c':
683               case 'C':
684                 if (!*tok->dptr)
685                     return -1;          /* premature end of token */
686                 mutt_buffer_addch(dest,
687                                   (toupper((unsigned char)*tok->dptr) - 'A' + 1) & 0x7f);
688                 tok->dptr++;
689                 break;
690               case 'r':
691                 mutt_buffer_addch(dest, '\r');
692                 break;
693               case 'n':
694                 mutt_buffer_addch(dest, '\n');
695                 break;
696               case 't':
697                 mutt_buffer_addch(dest, '\t');
698                 break;
699               case 'f':
700                 mutt_buffer_addch(dest, '\f');
701                 break;
702               case 'e':
703                 mutt_buffer_addch(dest, '\033');
704                 break;
705               default:
706                 if (isdigit((unsigned char)ch)
707                 &&  isdigit((unsigned char)*tok->dptr)
708                 &&  isdigit((unsigned char)*(tok->dptr + 1)))
709                 {
710                     mutt_buffer_addch(dest, (ch << 6) + (*tok->dptr << 3) +
711                                             *(tok->dptr + 1) - 3504);
712                     tok->dptr += 2;
713                 } else {
714                     mutt_buffer_addch(dest, ch);
715                 }
716             }
717         } else
718         if (ch == '^' && (flags & M_TOKEN_CONDENSE)) {
719             if (!*tok->dptr)
720                 return -1;              /* premature end of token */
721             ch = *tok->dptr++;
722             if (ch == '^') {
723                 mutt_buffer_addch(dest, ch);
724             } else
725             if (ch == '[') {
726                 mutt_buffer_addch(dest, '\033');
727             } else
728             if (isalpha((unsigned char)ch)) {
729                 mutt_buffer_addch(dest, toupper((unsigned char)ch) - 'A' + 1);
730             } else {
731                 mutt_buffer_addch(dest, '^');
732                 mutt_buffer_addch(dest, ch);
733             }
734         } else
735         if (ch == '`' && (!qc || qc == '"')) {
736             FILE *fp;
737             pid_t pid;
738             char *cmd, *ptr;
739             ssize_t expnlen;
740             BUFFER expn;
741             int line = 0;
742
743             pc = tok->dptr;
744             do {
745                 if ((pc = strpbrk(pc, "\\`"))) {
746                     /* skip any quoted chars */
747                     if (*pc == '\\')
748                         pc += 2;
749                 }
750             } while (pc && *pc != '`');
751             if (!pc) {
752                 return (-1);
753             }
754
755             cmd = p_dupstr(tok->dptr, pc - tok->dptr);
756             if ((pid = mutt_create_filter(cmd, NULL, &fp, NULL)) < 0) {
757                 p_delete(&cmd);
758                 return -1;
759             }
760             p_delete(&cmd);
761
762             tok->dptr = pc + 1;
763
764             /* read line */
765             p_clear(&expn, 1);
766             expn.data = mutt_read_line(NULL, &expn.dsize, fp, &line);
767             m_fclose(&fp);
768             mutt_wait_filter(pid);
769
770             /* if we got output, make a new string consiting of the shell ouptput
771                plus whatever else was left on the original line */
772             /* BUT: If this is inside a quoted string, directly add output to 
773              * the token */
774             if (expn.data && qc) {
775                 mutt_buffer_addstr(dest, expn.data);
776                 p_delete(&expn.data);
777             } else
778             if (expn.data) {
779                 expnlen = m_strlen(expn.data);
780                 tok->dsize = expnlen + m_strlen(tok->dptr) + 1;
781                 ptr = xmalloc(tok->dsize);
782                 memcpy(ptr, expn.data, expnlen);
783                 m_strcpy(ptr + expnlen, tok->dsize - expnlen, tok->dptr);
784                 if (tok->destroy)
785                     p_delete(&tok->data);
786                 tok->data = ptr;
787                 tok->dptr = ptr;
788                 tok->destroy = 1;       /* mark that the caller should destroy this data */
789                 ptr = NULL;
790                 p_delete(&expn.data);
791             }
792         } else
793         if (ch == '$' && (!qc || qc == '"')
794         && (*tok->dptr == '{' || isalpha((unsigned char)*tok->dptr)))
795         {
796             char *env = NULL, *var = NULL;
797
798             if (*tok->dptr == '{') {
799                 tok->dptr++;
800                 if ((pc = strchr (tok->dptr, '}'))) {
801                     var = p_dupstr(tok->dptr, pc - tok->dptr);
802                     tok->dptr = pc + 1;
803                 }
804             } else {
805                 for (pc = tok->dptr; isalnum((unsigned char)*pc) || *pc == '_';
806                      pc++);
807                 var = p_dupstr(tok->dptr, pc - tok->dptr);
808                 tok->dptr = pc;
809             }
810             if (var) {
811                 char tmp[STRING];
812                 if ((env = getenv (var))
813                 || (mutt_option_value (var, tmp, sizeof (tmp)) && (env = tmp)))
814                 {
815                     mutt_buffer_addstr (dest, env);
816                 }
817             }
818             p_delete(&var);
819         } else {
820             mutt_buffer_addch(dest, ch);
821         }
822     }
823     mutt_buffer_addch(dest, 0);  /* terminate the string */
824     tok->dptr = vskipspaces(tok->dptr);
825     return 0;
826 }